[R] Sorting in R
jimrc at gauss.math.montana.edu
Wed Jun 21 17:33:36 CEST 2000
On Wed, 21 Jun 2000, Art Salwin wrote:
> How do you sort one vector using another vector as a sort
> key? Sort of equivalent
> to sorting rows of a spreadsheet using one of its columns as
> key. Here's a simple example:
> age<-c(22, 57, 13)
> zipcode<-(50854, 20617, 91234)
For brevity, I'd make these into a data frame:
spsheet <- data.frame(name,age,zip)
> If we sort the name vector in zipcode order we should get
> mary, joe, sue
> If we sort name in age order we should get sue, joe, mary
> Having secondary keys to break ties would be nice, as would
> the ability to sort (in this example)
> all 3 vectors at once.
That's a bit harder, I would sort first on the secondary column, then
the primary. Or tertiary - secondary - primary.
If primary is name, secondary age, tertiary zip:
spsheet <- spsheet[order(zip),]
spsheet <- spsheet[order(age),]
I hope someone has a better way.
> Any help appreciated.
> Art Salwin
> salwin at mitretek.org
> (202) 863-2985
> (202) 863-2988 (fax)
> 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
Jim Robison-Cox ____________
Department of Math Sciences | | phone: (406)994-5340
2-214 Wilson Hall \ BZN, MT | FAX: (406)994-1789
Montana State University | *_______|
Bozeman, MT 59717-2400 \_| e-mail: jimrc at math.montana.edu
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