[R] Q: sort a matrix by picking up columns?
Renaud Lancelot
lancelot at telecomplus.sn
Thu Oct 14 11:53:26 CEST 1999
Hello Jan,
> do.call("order", data.frame(Mat[,c(1,3,6,8,10,13)]))
>
> NB do.call expects the name of the function to be called as the first argument
> and the arguments to that function in terms of a list. This list can (I think
> most easily) be obtained by using data.frame().
IMHO, this does not do what is needed:
> m <- matrix(c(9:5, c(1, 4, 3, 3, 5), c(1, 2, 4, 3, 5)), ncol = 3, byrow = F)
> m
[,1] [,2] [,3]
[1,] 9 1 1
[2,] 8 4 2
[3,] 7 3 4
[4,] 6 3 3
[5,] 5 5 5
> do.call("order", data.frame(m[, c(2, 3)]))
[1] 1 4 3 2 5
Using the same idea, we could write a small function:
> SortMat <- function(Mat, Sort)
{
m <- do.call("order", as.data.frame(Mat[, Sort]))
Mat[m, ]
}
> m
[,1] [,2] [,3]
[1,] 9 1 1
[2,] 8 4 2
[3,] 7 3 4
[4,] 6 3 3
[5,] 5 5 5
> SortMat(Mat = m, Sort = c(2, 3))
[,1] [,2] [,3]
[1,] 9 1 1
[2,] 6 3 3
[3,] 7 3 4
[4,] 8 4 2
[5,] 5 5 5
Hope this helps,
Renaud
--
Renaud Lancelot
ISRA-LNERV
BP 2057 Dakar-Hann
Senegal
tel (221) 832 49 02
fax (221) 821 18 79
email renaud.lancelot at cirad.fr
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
More information about the R-help
mailing list