[R] RE: aggregating dates

Paul Sorenson Paul.Sorenson at vision-bio.com
Tue Feb 1 01:19:07 CET 2005


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.

A second related problem I haven't been able to solve as yet is how to 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?




More information about the R-help mailing list