[R] R matrix sorting question

Huntsinger, Reid reid_huntsinger at merck.com
Thu May 19 20:11:55 CEST 2005

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.


R-help at stat.math.ethz.ch mailing list
PLEASE do read the posting guide!

More information about the R-help mailing list