按行排序矩阵行

时间:2016-01-10 22:06:09

标签: r matrix

我有一个矩阵m

m <- matrix(c(1, 8, 3, 1, 2, 4, 9, 0, 0), nrow = 3, byrow = TRUE)
m

     [,1] [,2] [,3]
[1,]    1    8    3
[2,]    1    2    4
[3,]    9    0    0

我计算rowMeans(m)

r.mean <- rowMeans(m)
r.mean

[1] 4.000000 2.333333 3.000000

如何使用r.mean将矩阵m从最大均值排序到最小值:

     [,1] [,2] [,3]
[1,]    1    8    3
[2,]    9    0    0
[3,]    1    2    4

1 个答案:

答案 0 :(得分:3)

像这样?

m[ order(rowMeans(m)), ]
     [,1] [,2] [,3]
[1,]    1    2    4
[2,]    9    0    0
[3,]    1    8    3

从最大平均值到最小值,添加, decreasing = T

m[ order(rowMeans(m), decreasing = T), ]
     [,1] [,2] [,3]
[1,]    1    8    3
[2,]    9    0    0
[3,]    1    2    4