HI, You can also use ?merge() Same example data from Milan: A <- matrix(runif(4*10), 10) rownames(A) <- LETTERS[1:10] B <- matrix(runif(10), 10) rownames(B) <- sample(LETTERS[1:10])
#Either, use C<-as.matrix(merge(A,B[order(rownames(B)),],by="row.names")) #or C<-as.matrix(merge(A,B[order(rownames(B)),],by=0)) row.names(C)<-C[,1] C<-C[,-1] colnames(C)<-NULL C A.K. ----- Original Message ----- From: Milan Bouchet-Valat <nalimi...@club.fr> To: Sridhar Iyer <sridi...@gmail.com> Cc: r-help@r-project.org Sent: Monday, September 10, 2012 3:22 AM Subject: Re: [R] R- merging two matrix Le dimanche 09 septembre 2012 à 19:29 -0500, Sridhar Iyer a écrit : > Hi, > > I have two matrices. (same size, same number of rows, > same names for the rows - one has data in sorted > order, another one master data in unsorted order. I need to > combine both). > > Rows names of Matrix A > P, Q, R, S, T, U, V ....... (some order) - each row has 4 values > > Row names of Matrix B (same as B, but in a different (sorted) order) > > Q, P, T, U, V, ....... (same names, but different order) - has one value > > I need to create a master matrix with data from these two. > > How do I take the numerical value for Q (from Matrix B) and append it to the > values in Matrix A. (so it becomes the 5th value in the master file) > > Thank you very much > > Srid Iyer 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 ______________________________________________ R-help@r-project.org mailing list 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. ______________________________________________ R-help@r-project.org mailing list 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.