Hi All I have a symmetric matrix of genes ( 100x100 matrix). I also have a matrix (100x2) of two columns where column 1 has the gene names and column 2 has the cluster it belongs to (they are sorted and grouped based on the cluster no). I would like to order the rows and columns of the 100x 100 matrix such that the first n genes correspond to cluster 1 and next n genes correspond to cluster 2 and so on. The order of genes is taken from the sorted matrix(100x2). Can someone tell me how to do this in R. I tried the grep() but I get a message saying that the length of pattern >1 so only first element will be compared. But i want to check for each gene in the 100x100 matrix for its cluster number and then group it. I also tried the order() but it did not help either. Thanks for the help! :) Aparna -- Aparna Sampath Master of Science (Bioinformatics) Nanyang Technological University Mob no : +65 91601854 [[alternative HTML version deleted]]
Combine the two matrices into one data.frame and order them Example done using data.frames rather than matrices but just use use data.frame(x,y) to convert to a data.frame bmat <- data.frame(matrix(1:25,5)) smat <- data.frame(aa= LETTERS[1:25], bb = rep(c("a","b","c", "d", "e"),5)) df1 <- data.frame(smat, bmat) orddata <- df1[order(df1[,2],decreasing=TRUE),] I hope this helps. --- On Sun, 6/26/11, Aparna Sampath <aparna.sampath26 at gmail.com> wrote:> From: Aparna Sampath <aparna.sampath26 at gmail.com> > Subject: [R] Ordering a matrix based on cluster no > To: r-help at r-project.org > Received: Sunday, June 26, 2011, 9:42 AM > Hi All > > I have a symmetric matrix of genes ( 100x100 matrix). I > also have a matrix > (100x2) of two columns where column 1 has the gene names > and column 2 has > the cluster it belongs to (they are sorted and grouped > based on the cluster > no). > > I would like to order the rows and columns of the 100x 100 > matrix such that > the first n genes correspond to cluster 1 and next n genes > correspond to > cluster 2 and so on. The order of genes is taken from the > sorted > matrix(100x2). > > Can someone tell me how to do this in R. > > I tried the grep() but I get a message saying that the > length of pattern >1 > so only first element will be compared. But i want to check > for each gene in > the 100x100 matrix for its cluster number and then group > it. > > I also tried the order() but it did not help either. > > Thanks for the help! :) > > Aparna > > -- > Aparna Sampath > Master of Science (Bioinformatics) > Nanyang Technological University > Mob no : +65 91601854 > > ??? [[alternative HTML version deleted]] > > ______________________________________________ > 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. >
Thanks for the help! But when I tried it, it does not work the same way I want. :( after combining the two matrices, they look like this: V1 V2 X TEL.AML1.C41 Hyperdip.50.C23 1 TEL.AML1.C41 1 TEL.AML1.C41 1.0000000 0.00000000 2 Hyperdip.50.C23 1 Hyperdip.50.C23 0.0000000 1.00000000 3 BCR.AB.LC1 1 BCR.AB.LC1 0.1212121 0.78125000 4 Hyperdip.50.C13 1 Hyperdip.50.C13 0.0000000 1.00000000 6 TEL.AML1.9 1 T.ALL.C5 0.0000000 0.03225807 7 TEL.AML1.8 1 TEL.AML1.9 1.0000000 0.00000000 8 Hyperdip.50.C7 1 TEL.AML1.8 1.0000000 0.00000000 9 TEL.AML1.C37 1 Hyperdip.50.C7 0.0000000 1.00000000 11 TEL.AML1.C47 1 TEL.AML1.C37 1.0000000 0.00000000 13 Hyperdip.50.11 1 MLL.6 0.0000000 0.03225807 when i do : orddata1 <- df2_a[order(df2_a[,1],decreasing=T),] I get the result: V1 V2 X TEL.AML1.C41 Hyperdip.50.C23 22 TEL.AML1.C49 1 TEL.AML1.2M.1 1 0.00000000 11 TEL.AML1.C47 1 TEL.AML1.C37 1 0.00000000 1 TEL.AML1.C41 1 TEL.AML1.C41 1 0.00000000 9 TEL.AML1.C37 1 Hyperdip.50.C7 0 1.00000000 6 TEL.AML1.9 1 T.ALL.C5 0 0.03225807 7 TEL.AML1.8 1 TEL.AML1.9 1 0.00000000 16 TEL.AML1.2M.4 1 Hyperdip.50.11 0 1.00000000 19 TEL.AML1.2M.1 1 TEL.AML1.2M.4 1 0.00000000 15 Hyperdip.50.R2 1 T.ALL.C10 0 0.00000000 20 Hyperdip.50.C9 1 BCR.ABL.Hyperdip.R5 0 1.00000000 The results are not right! I want it to look for the gene TEL.AML1.C49 in the second matrix and group it accordingly. Aparna -- View this message in context: http://r.789695.n4.nabble.com/Ordering-a-matrix-based-on-cluster-no-tp3625956p3627017.html Sent from the R help mailing list archive at Nabble.com.