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 © Togaware Pty Ltd
Support further development through the purchase of the PDF version of the book.
The PDF version is a formatted comprehensive draft book (with over 800 pages).
Brought to you by Togaware. This page generated: Sunday, 22 August 2010