[R] Sorting a matrix on two columns

Rau, Roland Rau at demogr.mpg.de
Mon Feb 21 11:07:15 CET 2005


Hi Glen,

 
-----Original Message-----
From: r-help-bounces at stat.math.ethz.ch
[mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Jones, Glen R

If a matrix with 5 columns has been defined and the first two columns
need to be sorted in ascending order, how can this be achieved whilst
ensuring the 
other 3 columns data are in relative position to the sorted columns?


does the following example-code help you?
mymatrix should resemble the structure of your (original) matrix.
mymatrix2 is the new, sorted matrix.

mymatrix <- matrix(runif(80), ncol=5)
mymatrix[,1] <- sample(c(1,2), size=length(mymatrix[,1]), replace=TRUE)
mymatrix

mymatrix2 <- mymatrix[order(mymatrix[,1],mymatrix[,2]),]
mymatrix2



Best,
Roland


+++++
This mail has been sent through the MPI for Demographic Rese...{{dropped}}




More information about the R-help mailing list