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

jim holtman jholtman at gmail.com
Thu Feb 19 17:51:02 CET 2009


How about this:

> x <- matrix(sample(0:1,100,TRUE),10)
> # create a list of all the columns to sort
> col.list <- lapply(seq(ncol(x)), function(a) x[,a])
> # now sort the matrix
> x[do.call(order, col.list),]
      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
 [1,]    0    0    1    0    0    1    1    0    0     1
 [2,]    0    0    1    0    1    0    1    1    1     1
 [3,]    0    1    0    1    0    1    1    1    1     1
 [4,]    0    1    0    1    1    0    0    0    0     1
 [5,]    1    0    1    0    0    0    0    1    0     0
 [6,]    1    0    1    1    0    0    0    1    0     0
 [7,]    1    0    1    1    0    1    1    1    0     1
 [8,]    1    1    0    0    0    1    0    1    1     0
 [9,]    1    1    0    0    1    1    1    1    0     0
[10,]    1    1    0    1    0    1    1    0    1     0


On Thu, Feb 19, 2009 at 11:40 AM, 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
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem that you are trying to solve?




More information about the R-help mailing list