0

I have a vector and a matrix . How can I get the following result?

         v = c(1, 3, 2, 4, 7, 5)
         v = sort(v)
         m = matrix(c(1,2, 3, 4,5, 6, 7, 8, 9, 10, 11, 12), ncol=2)
         > res = matrix(c(1, 3, 2, 4, 6, 5, 7, 9, 8, 10, 12, 11), ncol=2)
         > res
              [,1] [,2]
        [1,]    1    7
        [2,]    3    9
        [3,]    2    8
        [4,]    4   10
        [5,]    6   12
        [6,]    5   11
rose
  • 1,971
  • 7
  • 26
  • 32
  • ?sort should tell you about `index.return = TRUE`. You can access the index using `sort(v,index.return = TRUE)$idx`. That might help - I wasn't entirely sure what you were asking... – Andy Clifton Oct 30 '13 at 04:07
  • I want to sort the matrix m in terms of sorted vector v. – rose Oct 30 '13 at 04:08
  • Maybe rewrite your question; give us your data, what you were looking for, what you tried, and why it doesn't work? See http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example for some suggestions. – Andy Clifton Oct 30 '13 at 04:16

1 Answers1

2

You likely are looking for order instead of sort

m[order(v), ]

     [,1] [,2]
[1,]    1    7
[2,]    3    9
[3,]    2    8
[4,]    4   10
[5,]    6   12
[6,]    5   11
Ricardo Saporta
  • 54,400
  • 17
  • 144
  • 178