[R] Matching rows between 2 matrix of different sizes

Uwe Ligges ligges at statistik.uni-dortmund.de
Sun Feb 6 22:20:58 CET 2005


miguel wrote:

> 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


See ?merge (well, works on data.frames):
    merge(a, b, by=1, all=TRUE)

Uwe Ligges


> Thanks,
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! 
> http://www.R-project.org/posting-guide.html




More information about the R-help mailing list