[R] aggregate a Date column does not work?
Tan, Richard
RTan at panagora.com
Mon Nov 22 22:08:35 CET 2010
Yes, I meant something like one of these:
b <- aggregate(a$payday, list(a$name), max)
b$x <- as.Date('1970-01-01') + b$x
or
b$x <- as.Date(b$x, origin='1970-01-01')
Thanks.
-----Original Message-----
From: David Winsemius [mailto:dwinsemius at comcast.net]
Sent: Monday, November 22, 2010 3:58 PM
To: Tan, Richard
Cc: r-help at r-project.org
Subject: Re: [R] aggregate a Date column does not work?
On Nov 22, 2010, at 3:54 PM, Tan, Richard wrote:
> Thanks, add as.Date('1970-01-01') to the result column works.
But that should make them all the same date in 1970. Since aggregate
renames the date column to "x", this should work:
as.Date( aggregate(a$payday, list(a$name), max)$x )
[1] "2000-01-01" "1999-01-01"
>
> Richard
>
> -----Original Message-----
> From: David Winsemius [mailto:dwinsemius at comcast.net]
> Sent: Monday, November 22, 2010 3:51 PM
> To: Tan, Richard
> Cc: r-help at r-project.org
> Subject: Re: [R] aggregate a Date column does not work?
>
>
> 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
>
>
David Winsemius, MD
West Hartford, CT
More information about the R-help
mailing list