Ordering a matrix by another variable

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Ordering a matrix by another variable

Daniel Pick

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.

Dan

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: Ordering a matrix by another variable

Duncan Murdoch
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

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html