[R] an S idiom for ordering matrix by columns?

Gustaf Rydevik gustaf.rydevik at gmail.com
Thu Feb 19 17:52:51 CET 2009


On Thu, Feb 19, 2009 at 5:40 PM, Aaron Mackey <ajmackey at gmail.com> wrote:
> There's got to be a better way to use order() on a matrix than this:
>
>> y
>    2L-035-3 2L-081-23 2L-143-18 2L-189-1 2R-008-5 2R-068-15 3L-113-4
> 3L-173-2
> 398        1         1         2        2        1         1        2
> 2
> 857        1         1         2        2        1         2        2
> 2
> 911        1         1         2        2        1         2        2
> 2
> 383        1         1         2        2        1         1        2
> 2
> 639        1         2         2        1        2         2        1
> 2
> 756        1         2         2        1        2         2        1
> 2
>    3L-186-1 3R-013-7 3R-032-1 3R-169-10 X-002 X-087
> 398        1        2        2         2     1     2
> 857        1        2        2         2     1     2
> 911        1        2        2         2     1     2
> 383        1        2        2         2     1     2
> 639        2        2        1         2     1     2
> 756        2        2        1         2     1     2
>
>>
> y[order(y[,1],y[,2],y[,3],y[,4],y[,5],y[,6],y[,7],y[,8],y[,9],y[,10],y[,11],y[,12],y[,13],y[,14]),]
>    2L-035-3 2L-081-23 2L-143-18 2L-189-1 2R-008-5 2R-068-15 3L-113-4
> 3L-173-2
> 398        1         1         2        2        1         1        2
> 2
> 383        1         1         2        2        1         1        2
> 2
> 857        1         1         2        2        1         2        2
> 2
> 911        1         1         2        2        1         2        2
> 2
> 639        1         2         2        1        2         2        1
> 2
> 756        1         2         2        1        2         2        1
> 2
>    3L-186-1 3R-013-7 3R-032-1 3R-169-10 X-002 X-087
> 398        1        2        2         2     1     2
> 383        1        2        2         2     1     2
> 857        1        2        2         2     1     2
> 911        1        2        2         2     1     2
> 639        2        2        1         2     1     2
> 756        2        2        1         2     1     2
>
> Thanks for any suggestions!
>
> -Aaron
>


You mean something like this:
> test<-matrix(sample(1:4,100,replace=T),ncol=10)
> test[do.call(order,data.frame(test)),]

?

Regards,

Gustaf


-- 
Gustaf Rydevik, M.Sci.
tel: +46(0)703 051 451
address:Essingetorget 40,112 66 Stockholm, SE
skype:gustaf_rydevik




More information about the R-help mailing list