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')) + )> aname payday 1 Tom 1999-01-01 2 Tom 2000-01-01 3 Pete 1998-01-01 4 Pete 1999-01-01> aggregate(a$payday, list(a$name), max)Group.1 x 1 Tom 10957 2 Pete 10592 Thanks, Richard [[alternative HTML version deleted]]
On Nov 22, 2010, at 3:39 PM, Tan, Richard wrote:> Hi, I am trying to aggregate max a Date type column but have weird > result, how do I fix this?In the process of getting max() you coerced the Dates to numeric and now you need to re-coerce them back to Dates ?as.Date as.Date(<your result>) (possibly with an origin it the default "1970-01-01" doesn't get used. -- David.> > > >> 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')) > > + ) > >> a > > name payday > > 1 Tom 1999-01-01 > > 2 Tom 2000-01-01 > > 3 Pete 1998-01-01 > > 4 Pete 1999-01-01 > >> aggregate(a$payday, list(a$name), max) > > Group.1 x > > 1 Tom 10957 > > 2 Pete 10592 > > > > Thanks, > > Richard > > > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.David Winsemius, MD West Hartford, CT
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