[R] Sorting a matrix on two columns

davidr@rhotrading.com davidr at rhotrading.com
Tue Feb 22 20:47:53 CET 2005


Perhaps I read more into the question than others, but
I thought he was asking about a stable sort. If that is
the case, ?order says that the sort is stable except for method =
"quick".

David L. Reiner

-----Original Message-----
From: Rau, Roland [mailto:Rau at demogr.mpg.de] 
Sent: Monday, February 21, 2005 4:07 AM
To: Jones, Glen R; r-help at stat.math.ethz.ch
Subject: RE: [R] Sorting a matrix on two columns

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}}

______________________________________________
R-help at stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide!
http://www.R-project.org/posting-guide.html




More information about the R-help mailing list