[R] Ordering a matrix by another variable

Duncan Murdoch murdoch at stats.uwo.ca
Mon Jan 2 23:32:37 CET 2006


On 1/2/2006 5:26 PM, Daniel Pick wrote:
 > Hello,
 >     Given the matrix M
 >     1 5     4
 >     2       4     6
 >     3       8             5
 >     4         2              7
 >
 > and the vector V 4,2,1,3,   I would like to order the rows in M according
 > to the indices in V, that is, I want output
 >
 >     4 2     7
 >     2 4     6
 >     1 5     4
 >     3 8     5
 >
 > How do I do this?  This is not a standard ascending or descending sort.

I think M[V,] is all you need.

Duncan Murdoch




More information about the R-help mailing list