Skip to content
Prev 305104 / 398506 Next

R- merging two matrix

Le dimanche 09 septembre 2012 ? 19:29 -0500, Sridhar Iyer a ?crit :
A small reproducible example would have been nice. Anyways, let's build
two matrices:
A <- matrix(runif(4*10), 10)
rownames(A) <- LETTERS[1:10]
B <- matrix(runif(10), 10)
rownames(B) <- sample(LETTERS[1:10])

Then you can just do:
C <- cbind(A, B[match(rownames(A), rownames(B)),])

or, if A is sorted by row names (depends on the locale...) :
D <- cbind(A, B[order(rownames(B)),])

Regards