Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any way to sort columns of a matrix independently in R?

Tags:

r

matrix

I'm a newbie in R so, I really need some help here. I just want to sort each column independently. Any help is appreciated!

 > mat <- matrix(c(45,34,1,3,4325,23,1,2,5,7,3,4,32,734,2),ncol=3)
 > mat
      [,1] [,2] [,3]
 [1,]   45   23    3
 [2,]   34    1    4
 [3,]    1    2   32
 [4,]    3    5  734
 [5,] 4325    7    2

to

      [,1] [,2] [,3]
 [1,]    1    1    2
 [2,]    3    2    3
 [3,]   34    5    4
 [4,]   45    7   32
 [5,] 4325    23 734
like image 328
Steve Hwang Avatar asked Oct 30 '12 13:10

Steve Hwang


1 Answers

Yes, there is!

apply(mat, 2, sort)
     [,1] [,2] [,3]
[1,]    1    1    2
[2,]    3    2    3
[3,]   34    5    4
[4,]   45    7   32
[5,] 4325   23  734
like image 72
Jilber Urbina Avatar answered Oct 03 '22 05:10

Jilber Urbina