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

C Lin baccts at hotmail.com
Mon Sep 28 20:46:18 CEST 2015

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))
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')

       x1   x2
y1  0.1  0.2
y2  0.3  0.3
y3  0.1  0.1

       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.

           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

More information about the R-help mailing list