我有一个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

最佳答案

像这样?

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

07-24 20:45