[R] How to preserve date format while aggregating

Gabor Grothendieck ggrothendieck at gmail.com
Tue Sep 9 01:06:11 CEST 2008


min does work for POSIXct and Date too:

> ct <- ISOdatetime(2008, 1, 1:10, 0, 0, 0)

> min(ct)
[1] "2008-01-01 EST"

> min(as.Date(ct))
[1] "2008-01-01"



On Mon, Sep 8, 2008 at 6:57 PM, Adam D. I. Kramer
<adik-rhelp at ilovebacon.org> wrote:
> Hi Erich,
>
> Since min() is defined for numbers and not dates, the problem is in the
> min() function. min() is converting from date format to number format.
>
> Your best bet is to make this conversion explicit...such that it is
> reversable. So, convert the date into UTC, then UTC to seconds since epoch,
> then take the minimum, then convert back to UTC time.
>
> This sounds like a pain...but that's basically what a version of min()
> designed to work with dates would do. The reason this is a pain is basically
> due to timezones:
>
> Consider a comparison between x = 3:54 PM September 8 in California (right
> now where I am) and y = 12:54 AM September 9 in Zurich (right now where you
> are). Is it earlier here than there? Yes, because it's Sept 8 to your Sept
> 9. Is it earlier there than here? Yes, because your day started 56 minutes
> ago, mine over 15 hours ago. Is it the same time here than there? Yes,
> because our UTC times are equal.
>
> So it's not clear what min should return, so min is not defined for dates.
> However, min is defined for numbers, and dates can be converted to
> numbers...but what those numbers actually mean is not necessarily clear.
>
> --Adam
>
> On Mon, 8 Sep 2008, Erich Studerus wrote:
>
>> Hi
>>
>> I have a dataframe in which some subjects appear in more than one row. I
>> want to extract the subject-rows which have the minimum date per subject.
>> I
>> tried the following aggregate function.
>>
>> attach(dataframe.xy)
>>
>> aggregate(Date,list(SubjectID),min)
>>
>> Unfortunately, the format of the Date-column changes to numeric, when I'm
>> applying this function. How can I preserve the date format?
>>
>> Thanks
>>
>> Erich
>>
>> ______________________________________________
>> 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.
>>
>
> ______________________________________________
> 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.
>



More information about the R-help mailing list