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