Grouping in R using data.table

Check out http://www.ats.ucla.edu/stat/r/faq/firstlast.htm

Now compare the steps below.

> options(prompt="R=> ")
R=> hsb2 <- read.csv('http://www.ats.ucla.edu/stat/r/faq/hsb2.csv')
R=> library(data.table) # ?data.table
R=>
R=> hsb2dt <- as.data.table(hsb2)
R=> setkey(hsb2dt, math) # sort by math... See ?setkey
R=>
R=> highest <- hsb2dt[, tail(.SD, 1), by=prog] # ?tail
R=> highest
   prog  id female race ses schtyp read write math science socst
1:    3 143      0    4   2      1   63    63   75      72    66
2:    1 169      0    4   1      1   55    59   63      69    46
3:    2 200      0    4   2      2   68    54   75      66    66
R=>
R=> lowest <- hsb2dt[, head(.SD, 1), by=prog]
R=> lowest
   prog  id female race ses schtyp read write math science socst
1:    3   2      1    1   2      1   39    41   33      42    41
2:    1 167      0    4   2      1   63    49   35      66    41
3:    2 128      0    4   3      1   39    33   38      47    41
R=>
R=> quit()

Checkout data.table at https://github.com/Rdatatable/data.table/wiki