3

I have a 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

I calculate the rowMeans(m) :

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

[1] 4.000000 2.333333 3.000000

How can I use r.mean to sort my matrix m from the maximum mean to the minimum :

     [,1] [,2] [,3]
[1,]    1    8    3
[2,]    9    0    0
[3,]    1    2    4
jbaums
  • 27,115
  • 5
  • 79
  • 119
Bilal
  • 2,883
  • 5
  • 37
  • 60

1 Answers1

5

like this?

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

From the maximum mean to the minimum, by adding , decreasing = T

m[ order(rowMeans(m), decreasing = T), ]
     [,1] [,2] [,3]
[1,]    1    8    3
[2,]    9    0    0
[3,]    1    2    4
Eric Fail
  • 8,191
  • 8
  • 72
  • 128