Dear R users, I am trying to merge tables based on both their row names and column names. My ultimate goal is to build a distribution table of values for each combination of row and column names. I have more test tables, more x's and y's than in the toy example below. Thanks in advance for your help. For example : test1 <- data.frame(rbind(c(0.1,0.2),0.3,0.1)) rownames(test1)=c('y1','y2','y3') colnames(test1) = c('x1','x2'); test2 <- data.frame(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') test1 x1 x2 y1 0.1 0.2 y2 0.3 0.3 y3 0.1 0.1 test2 x1 x3 x2 y2 0.8 0.9 0.5 y5 0.5 0.1 0.6 I would like to combine test1 and test2 such that if the column name and row name are both the same they are combined. combined_test x1 x2 x3 y1 0.1 0.2 NA y2 (0.3,0.8) (0.3,0.5) 0.9 y3 0.1 0.1 NA y5 0.5 0.6 0.1
Frank Schwidom
2015-Sep-28 21:23 UTC
[R] merging tables based on both row and column names
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') lTest12 <- list( test1, test2) namesRow <- unique( unlist( lapply( lTest12, rownames))) namesCol <- unique( unlist( lapply( lTest12, colnames))) tmp1 <- do.call( cbind, lapply( lTest12, function( x) as.vector( x[ match( namesRow, rownames( x)), match( namesCol, colnames( x))]))) tmp2 <- apply( tmp1, 1, list) tmp3 <- lapply( tmp2, function( x) x[[1]][ !is.na( x[[1]])]) dimnames1 <- list( namesRow, namesCol) tmp4 <- array( data= tmp3, dim= sapply( dimnames1, length), dimnames= dimnames1) paste( tmp4) [1] "0.1" "c(0.3, 0.8)" "0.1" "0.5" "0.2" [6] "c(0.3, 0.5)" "0.1" "0.6" "numeric(0)" "0.9" [11] "numeric(0)" "0.1" tmp4 x1 x2 x3 y1 0.1 0.2 Numeric,0 y2 Numeric,2 Numeric,2 0.9 y3 0.1 0.1 Numeric,0 y5 0.5 0.6 0.1 Regards. On 2015-09-28 18:46:18, C Lin wrote:> Dear R users, > > I am trying to merge tables based on both their row names and column names. > My ultimate goal is to build a distribution table of values for each combination of row and column names. > I have more test tables, more x's and y's than in the toy example below. > Thanks in advance for your help. > > For example : > test1 <- data.frame(rbind(c(0.1,0.2),0.3,0.1)) > rownames(test1)=c('y1','y2','y3') > colnames(test1) = c('x1','x2'); > test2 <- data.frame(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') > > test1 > x1 x2 > y1 0.1 0.2 > y2 0.3 0.3 > y3 0.1 0.1 > > test2 > x1 x3 x2 > y2 0.8 0.9 0.5 > y5 0.5 0.1 0.6 > > I would like to combine test1 and test2 such that if the column name and row name are both the same they are combined. > > combined_test > x1 x2 x3 > y1 0.1 0.2 NA > y2 (0.3,0.8) (0.3,0.5) 0.9 > y3 0.1 0.1 NA > y5 0.5 0.6 0.1 > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >