Togaware DATA MINING
Desktop Survival Guide
by Graham Williams
Google

Sorting

To sort a matrix on two columns us the order function:

x <- rep(rbinom(5, size=4, prob=.5), 6)
y <- rnorm(30)
df <- data.frame(x=x, y=y)
df <- df[order(df$x, df$y),]

To reverse sort on the second sort column:

df <- df[order(df$x, -df$y),]

For dealing with missing values in a sort, see http://www.ats.ucla.edu/STAT/r/faq/sort.htm.



Copyright © 2004-2006 Graham.Williams@togaware.com
Support further development through the purchase of the PDF version of the book.
Brought to you by Togaware.