[R] Equivalent of 'first.var' or 'last.var' from SAS in R?

hadley wickham h.wickham at gmail.com
Thu Sep 25 22:05:23 CEST 2008


On Thu, Sep 25, 2008 at 2:00 PM, Matthew Pettis
<matthew.pettis at gmail.com> wrote:
> Hi,
>
> I want to sort a data frame by multiple columns and then take the
> first record in each unique level of the "by" group I used to sort the
> data frame.  Does someone have an example of how to do this?
>
> Thanks,
> Matt

In the (very soon to be released) plyr package, you can do:

library(plyr)
ddply(airquality, .(Month), head, 1)
ddply(airquality, .(Month), tail, 1)

Hadley

-- 
http://had.co.nz/



More information about the R-help mailing list