E-mail # 2 / attachments matrix test cases
HI Sridhar, You may not need to order the dataset by the "X" to do the merge. It's already ordered. Combinedat1<-merge(testMaster1,test2Sorted1,by="X") ?new1<-merge(testMaster,test2Sorted,by="X") ?identical(Combinedat1,new1) #[1] TRUE ? A.K. ----- Original Message ----- From: Sridhar Iyer <sridiyer at gmail.com> To: arun <smartpink111 at yahoo.com> Cc: Milan Bouchet-Valat <nalimilan at club.fr> Sent: Monday, September 10, 2012 8:05 AM Subject: E-mail # 2 / attachments matrix test cases forgot to attach the sample files. sorry.
On Mon, Sep 10, 2012 at 7:02 AM, Sridhar Iyer <sridiyer at gmail.com> wrote:
Hi Milan & Arun, Thank you so much for the response. Sorry I did not include a test file. (New to R,? did not realize the importance of enclosing a test case - it does make sense). test1Master.xls? - has some data (from a set of experiments)? Row 1 (row names are genes) ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? row names in some random order test2sorted.xls? - has data from a new experiment. ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? row names in a different order (but genes are same as in test1Master file ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? only the oder is different) I need to append data from the new experiment (test2Sorted) into test1Master file. Thanks Srid On Mon, Sep 10, 2012 at 6:51 AM, arun <smartpink111 at yahoo.com> wrote:
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 <nalimilan at club.fr> To: Sridhar Iyer <sridiyer at gmail.com> Cc: r-help at 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 at 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.