[R] How to preserve date format while aggregating

Prof Brian Ripley ripley at stats.ox.ac.uk
Tue Sep 9 08:57:43 CEST 2008


This is completely wrong: min _is_ defined for date-times:

> min(.leap.seconds)
[1] "1972-07-01 01:00:00 BST"

Please do study the posting guide and do your homework before posting: you 
seem unaware of what the POSIXct class is, so ?DateTimeClasses is one 
place you need to start.  And

> methods("Summary")
[1] Summary.data.frame      Summary.Date            Summary.difftime
[4] Summary.factor          Summary.numeric_version Summary.POSIXct
[7] Summary.POSIXlt

so ?Summary is another.

On Mon, 8 Sep 2008, Adam D. I. Kramer 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.
>

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595



More information about the R-help mailing list