[R] aggregate a Date column does not work?

Gabor Grothendieck ggrothendieck at gmail.com
Mon Nov 22 22:13:11 CET 2010


On Mon, Nov 22, 2010 at 3:39 PM, Tan, Richard <RTan at panagora.com> wrote:
> Hi, I am trying to aggregate max a Date type column but have weird
> result, how do I fix this?
>
>> a <- rbind(
>
> + data.frame(name='Tom', payday=as.Date('1999-01-01')),
> + data.frame(name='Tom', payday=as.Date('2000-01-01')),
> + data.frame(name='Pete', payday=as.Date('1998-01-01')),
> + data.frame(name='Pete', payday=as.Date('1999-01-01'))
> + )
>


Since its already sorted try this:

a[!duplicated(a$name, fromLast = TRUE), ]

Using sqldf also works:

library(sqldf)
sqldf("select name, max(payday) payday from a group by name order by name")


-- 
Statistics & Software Consulting
GKX Group, GKX Associates Inc.
tel: 1-877-GKX-GROUP
email: ggrothendieck at gmail.com



More information about the R-help mailing list