[R] merging tables based on both row and column names

Giorgio Garziano giorgio.garziano at ericsson.com
Thu Oct 1 21:23:08 CEST 2015


Replacing na.omit() with !is.na() appears to improve performance with time.

  rm(list=ls())

  test1 <- (rbind(c(0.1,0.2),0.3,0.1))
  rownames(test1)=c('y1','y2','y3')
  colnames(test1) = c('x1','x2');
  test2 <- (rbind(c(0.8,0.9,0.5),c(0.5,0.1,0.6)))
  rownames(test2) = c('y2','y5')
  colnames(test2) = c('x1','x3','x2')

  solution_3 <- function(test1, test2) {
    lTest12 <- list(test1, test2)
    namesRow <- unique( unlist( lapply(lTest12, rownames)))
    namesCol <- unique( unlist( lapply(lTest12, colnames)))
    tmp1 <- sapply(lTest12, function(x) as.vector(x[match(namesRow, rownames(x)), match(namesCol, colnames(x))]))
    tmp2 <- apply(tmp1, 1, function(x) { x[!is.na(x)] })
    dimnames1 <- list(namesRow, namesCol)
    tmp3 <- array(data = tmp2, dim = sapply(dimnames1, length), dimnames = dimnames1)
    tmp3
  }

  system.time(for(i in 1:10000) {solution_3(test1, test2)})




	[[alternative HTML version deleted]]



More information about the R-help mailing list