[R] sorting

Duncan Murdoch murdoch at stats.uwo.ca
Sun Jan 20 16:53:40 CET 2008


On 20/01/2008 10:50 AM, threshold wrote:
> Hi, I want to sort my matrix according to (any) selected column. For example
> given matrix:
>> x
>      [,1] [,2] [,3]
> [1,]    3    4    7
> [2,]    2    5    8
> [3,]    1    6    9
> 
> I want to sort the first column in ascending order and make the other
> columns follow the 'new order' like:
> 
>> x1
>      [,1] [,2] [,3]
> [1,]    1    6    9
> [2,]    2    5    8
> [3,]    3    4    7
> 
> thank in advance for help.

See ?order.

o <- order(x[,1])
x1 <- x[o,]

Duncan Murdoch



More information about the R-help mailing list