有没有办法在R中独立地对矩阵的列进行排序?

时间:2012-10-30 13:36:03

标签: r matrix

我是R的新手,所以我真的需要一些帮助。我只想独立地对每列进行排序。任何帮助表示赞赏!

 > 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

      [,1] [,2] [,3]
 [1,]    1    1    2
 [2,]    3    2    3
 [3,]   34    5    4
 [4,]   45    7   32
 [5,] 4325    23 734

1 个答案:

答案 0 :(得分:15)

是的,有!

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