Dear there, I am trying to do the following stuff. Could you let me know how to do it efficiently? > aaa [,1] [,2] [1,] 1 -0.2 [2,] 3 0.8 [3,] 4 0.3 [4,] 5 0.2 [5,] 7 0.9 And I would like to sort the matrix by column 2 (and accordingly column 1 sorted as well). The desired matrix will be 1 -0.2 5 0.2 4 0.3 3 0.8 7 0.9 If using Excel or SAS, I can do this easily, but I couldn't figure out how to do it in R. Could you let me know the efficient way to do this ? I appreciate if the solution or reply comes as quickly as possible. Thank you very much. Insu
You can use "order" as follows:> p <- order(aaa[,2]) > aa[p,]p is the permutation putting aaa[,2] in ascending order, then aaa[p,] reorders the rows according to p. Reid Huntsinger -----Original Message----- From: r-help-bounces at stat.math.ethz.ch [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of insu_paek at harcourt.com Sent: Thursday, May 19, 2005 1:43 PM To: R-help at stat.math.ethz.ch Subject: [R] R matrix sorting question Dear there, I am trying to do the following stuff. Could you let me know how to do it efficiently? > aaa [,1] [,2] [1,] 1 -0.2 [2,] 3 0.8 [3,] 4 0.3 [4,] 5 0.2 [5,] 7 0.9 And I would like to sort the matrix by column 2 (and accordingly column 1 sorted as well). The desired matrix will be 1 -0.2 5 0.2 4 0.3 3 0.8 7 0.9 If using Excel or SAS, I can do this easily, but I couldn't figure out how to do it in R. Could you let me know the efficient way to do this ? I appreciate if the solution or reply comes as quickly as possible. Thank you very much. Insu ______________________________________________ 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
Try:> A=matrix(c(1,3,4,5,7, -0.2, 0.8, 0.3, 0.2, 0.9 ),5,2) > A[,1] [,2] [1,] 1 -0.2 [2,] 3 0.8 [3,] 4 0.3 [4,] 5 0.2 [5,] 7 0.9> A[order(A[,2]), ][,1] [,2] [1,] 1 -0.2 [2,] 5 0.2 [3,] 4 0.3 [4,] 3 0.8 [5,] 7 0.9 Jarek ====================================================\====== Jarek Tuszynski, PhD. o / \ Science Applications International Corporation <\__,| (703) 676-4192 "> \ Jaroslaw.W.Tuszynski at saic.com ` \ -----Original Message----- From: r-help-bounces at stat.math.ethz.ch [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of insu_paek at harcourt.com Sent: Thursday, May 19, 2005 1:43 PM To: R-help at stat.math.ethz.ch Subject: [R] R matrix sorting question Dear there, I am trying to do the following stuff. Could you let me know how to do it efficiently? > aaa [,1] [,2] [1,] 1 -0.2 [2,] 3 0.8 [3,] 4 0.3 [4,] 5 0.2 [5,] 7 0.9 And I would like to sort the matrix by column 2 (and accordingly column 1 sorted as well). The desired matrix will be 1 -0.2 5 0.2 4 0.3 3 0.8 7 0.9 If using Excel or SAS, I can do this easily, but I couldn't figure out how to do it in R. Could you let me know the efficient way to do this ? I appreciate if the solution or reply comes as quickly as possible. Thank you very much. Insu ______________________________________________ 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