我有一个矩阵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
答案 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