RE: [R] R matrix sorting question

From: Tuszynski, Jaroslaw W. <JAROSLAW.W.TUSZYNSKI_at_saic.com>
Date: Fri 20 May 2005 - 04:30:05 EST


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@saic.com                     `    \



-----Original Message-----
From: r-help-bounces@stat.math.ethz.ch
[mailto:r-help-bounces@stat.math.ethz.ch] On Behalf Of
insu_paek@harcourt.com
Sent: Thursday, May 19, 2005 1:43 PM
To: R-help@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@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

R-help@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 Received on Fri May 20 04:41:32 2005

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:31:55 EST