[R] RE: aggregating dates

Gabor Grothendieck ggrothendieck at myway.com
Tue Feb 1 04:48:02 CET 2005


Paul Sorenson <Paul.Sorenson <at> vision-bio.com> writes:

: 
: The solution I came up with myself was simply to coerce the integer back to 
POSIXct:
: 
: 	class(ev$date) = "POSIXct"
: 
: Can't say it is the right way to do it but it seem to work.

That is not entirely correct, although it many cases it will work,
since its also a subclass of POSIXt.  

The RNews article I mentioned in my original response to your
query has the correct solution.

: 
: A second related problem I haven't been able to solve as yet is how to

My original response to your query already had a solution.



: include "incidents" columns (those not
: in 'x' or 'by') in an aggregate.
: 
: 	names(ev): "date" "defectnum" "state"
: 
: 	aggregate(ev$date, by=list(ev$defectnum), max)
: 
: This returns only the date and defectnum, I also need the state.
: 
: I tried writing my own aggregator function:
: 	maxevent = function(events) {
: 	    events[which.max(events$date),]
: 	}
: 
: 	aggregate(ev, by=list(ev$defectnum), maxevent)
: 
: But I get:
: 
: 	Error in "[.default"(events, which.max(events$date), ) : 
:       	  incorrect number of dimensions
: 
: I am trying to retrieve only the rows of ev with the latest date for a given 
defectnum.
: 
: cheers
: 
: > Message: 29
: > Date: Mon, 31 Jan 2005 16:16:35 +1100
: > From: "Paul Sorenson" <Paul.Sorenson <at> vision-bio.com>
: > Subject: [R] aggregating dates
: > To: <r-help <at> stat.math.ethz.ch>
: > Message-ID: <5E06BFED29594F4C9C5EBE230DE320C6068027CD <at> ewok.vsl.com.au>
: > Content-Type: text/plain;	charset="iso-8859-1"
: > 
: > I have a frame which contains 3 columns:
: > 
: > "date" "defectnum" "state"
: > 
: > And I want to get the most recent state change for a given 
: > defect number.  date is POSIXct.
: > 
: > I have tried:
: > 	aggregate(ev$date, by=list(ev$defectnum), max)
: > 
: > Which appears to be working except that the dates seem to 
: > come back as integers (presumably the internal representation 
: > of POSIXct).
: > 
: > When I execute max(ev$date) the result remains POSIXct.
: > 
: > I have been dredging through the help among DateTimeClasses 
: > and haven't found a function that converts these integers to 
: > some kind of date class.  Or a method for using aggregate 
: > which doesn't perform the conversion in the first place.
: > 
: > Any clues?
: 
: ______________________________________________
: R-help <at> stat.math.ethz.ch mailing list
: https://stat.ethz.ch/mailman/listinfo/r-help
: PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
: 
:




More information about the R-help mailing list