Sorting rows in a matrix based on vector of indecies
Hi David, This was useful, thanks. The example was just that, there are no "a"s, "b",s etc I was just trying to show what I was trying to do. Using m[v,] where m was the matrix in my example and v the vector of index values works great as you suggested Thanks. R is quite a powerful language as I am starting to discover (and folks on the list here are very helpful). Regards, Esmail
David Winsemius wrote:
See if this helps:
> mtx<-matrix( rep(letters[1:4], 6), nrow=4) > mtx
[,1] [,2] [,3] [,4] [,5] [,6] [1,] "a" "a" "a" "a" "a" "a" [2,] "b" "b" "b" "b" "b" "b" [3,] "c" "c" "c" "c" "c" "c" [4,] "d" "d" "d" "d" "d" "d"
> mtx[c(2, 4, 1, 3), ]
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] "b" "b" "b" "b" "b" "b"
[2,] "d" "d" "d" "d" "d" "d"
[3,] "a" "a" "a" "a" "a" "a"
[4,] "c" "c" "c" "c" "c" "c"
You can do with that result whatever you want:
assign it, m2 <- mtx[c(2, 4, 1, 3), ]