[R] sort dates within a factor

hadley wickham h.wickham at gmail.com
Tue Sep 29 16:30:58 CEST 2009


On Tue, Sep 29, 2009 at 6:58 AM,  <xavier.chardon at free.fr> wrote:
> Apologies for the misunderstanding. I can come up with a solution that might suit your needs:
>
> library(plyr)
> out <- ddply(test, .(nr), function(x) data.frame(date=x$date, index=rank(-as.integer(x$date))))
> out[is.na(out$nr) | is.na(out$date), "index"] <- NA

That code can be simplified a little to:

ddply(test, .(nr), transform, index = rank(-as.integer(date), na.last = "keep"))

Hadley

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




More information about the R-help mailing list