[R] RE: aggregating dates

Gabor Grothendieck ggrothendieck at myway.com
Tue Feb 1 14:41:15 CET 2005


The issue is just that you need to add POSIXt to the class vector to
be absolutely correct.

   class(x) <- c("POSIXt", "POSIXct")

is the same as

   structure(x, class = c("POSIXt", "POSIXct"))

The only reason the table used the latter was to put it in functional
form rather than lvalue form.  The class form that you are using does
have the advantage of not manipulating the underlying representation
directly.  The way that class(x) <- "POSIXct" might ail would be if there 
were a POSIXt method that should be applied since it would then never
find it.  I don't know of an actual example of this which is why
I mentioned that it will mostly work anyways but its possible that
there are situations where it could fail, at least in in principle.

Regarding the question below on W. Australia Standard Time, POSIXct
stores the datetime internally relative to GMT regardless of how you
intend to use or display it (although it does have the capability of
storing the time zone using the tzone attribute so it can know which 
time zone to operate in when there might be confusion).  

Also note that if you are using fBasics then fBasics does its own
time zone processing using the Olsen data base and only uses POSIXct
as an underlying layer for its GMT processing.  Also, if you use fBasics 
be sure you have set your process or computer to GMT time for it to 
process the times correctly.

Mulholland, Tom <Tom.Mulholland <at> dpi.wa.gov.au> writes:

: 
: I am probably just displaying my ignorance, but I have obviously managed to 
miss exactly what you are
: referring to.
: 
: Firstly I have to thank you for making me look closer at the article. I had 
done so but I had obviously skipped
: over the Comparison table, which I would have found useful in the past. I, 
like many others have struggled
: with dates and because I use fSeries etc I have started to used POSIXct as 
my default way of including dates
: within my datasets.  (That's where the table might have stopped me if I had 
read it the first time around)
: 
: But my issue is that you say that the article tells you how to convert from 
an Integer into POSIXct. In case it
: makes a difference I think the question is how do you make 1104508800 
into "2005-01-01 W. Australia
: Standard Time"
: 
: Is the answer, structure(1104508800,class = c("POSIXt","POSIXct"))?
: 
: Following Paul's message I tried 
: 
: > y <- 1104508800
: > class(y) = "POSIXct"
: > y
: [1] "2005-01-01 W. Australia Standard Time"
: > str(y)
: Class 'POSIXct'  num 1104508800
: 
: > y <- ISOdate(2005,1,1)
: > str(y)
: `POSIXct', format: chr "2005-01-01 12:00:00"
: 
: So checking the table in your article I tought this may shed some light on 
the topic
: 
: > newy <- structure(1104508800,class = c("POSIXt","POSIXct"))
: > newy
: [1] "2005-01-01 W. Australia Standard Time"
: > str(newy)
: `POSIXct', format: chr "2005-01-01"
: 
: It's the same format as ISOdate so I assume it's the right way to do the 
conversion.
: 
: You suggested that the simple 'class(y) = "POSIXct"' might fail. Since I 
thought that maybe plot functions
: might be interfered with I ran
: 
: > x <- runif(3)
: > y <- as.POSIXct(c("2005-01-01","2005-01-02","2005-01-03"))
: > plot(y,x)
: NULL
: > y <- as.numeric(as.POSIXct(c("2005-01-01","2005-01-02","2005-01-03")))
: > class(y) = "POSIXct"
: > plot(y,x)
: NULL
: 
: The plots seemed to be both the same so I am not sure what the implications 
of your statement is.
: 
: I hope this makes sense, because as usual the topic seems to go around in 
circles. It all makes complete sense
: and nonsense at the same time. Because rightly or wrongly it seems to me 
that class(y) <-
: c("POSIXt","POSIXct") is the same thing as the structure statement. So is it 
just failing to include the
: POSIXt that is the issue.
: 
: > 
: 
: > -----Original Message-----
: > From: Gabor Grothendieck [mailto:ggrothendieck <at> myway.com]
: > Sent: Tuesday, 1 February 2005 11:48 AM
: > To: r-help <at> stat.math.ethz.ch
: > Subject: Re: [R] RE: aggregating dates
: > 
: > 
: > 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
: > : 
: > :
: > 
: > ______________________________________________
: > 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
: >
: 
: ______________________________________________
: 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