[R] Matching rows between 2 matrix of different sizes

miguel miguel at aei.ca
Sun Feb 6 21:28:27 CET 2005


Hi, I would like is to add b's columns when a's first col. matches with 
b's first col. (a+b if a[i,1] == b[i,1]):

a <- matrix(c(11,22,33,4:9),nrow=3)
a
     [,1] [,2] [,3]
[1,]   11    4    7
[2,]   22    5    8
[3,]   33    6    9

b <- matrix(c(11,22,3:4),nrow=2)
b
     [,1] [,2]
[1,]   11    3
[2,]   22    4

The desired answer :
     [,1] [,2] [,3] [,4]
[1,]   11    4    7    3
[2,]   22    5    8    4
[3,]   33    6    9   NA

Thanks,




More information about the R-help mailing list