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?
This seems to work
toPOSIX <- function(x){
y <- x - as.numeric(ISOdate(2005,1,1))
z <- ISOdate(2005,1,1) + y
return(z)
}
test <- as.numeric(ISOdate(2005,3,1) )
toPOSIX(test)
But whether one should be doing this I don't know. There are certainly
functions that play aorund with the POSIX format, I have always assumed that
I'm missing something fundamental. But I have always found it easier to
write a function like this than to find out what I should have done to keep the
format intact in the first place.
Tom
The caveat is the critical or otherwise nature of the endeavour. If it matters,
find the right answer or convince yourself that this code works.
> -----Original Message-----
> From: Paul Sorenson [mailto:Paul.Sorenson at vision-bio.com]
> Sent: Monday, 31 January 2005 1:17 PM
> To: r-help at stat.math.ethz.ch
> Subject: [R] aggregating dates
>
>
> 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
>
Paul Sorenson <Paul.Sorenson <at> vision-bio.com> writes: : : 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 RNews 4/1 has an article with this and other date conversions. : which doesn't perform the : conversion in the first place. You could sort ev by date and use aggregate on an index vector (untested): ev <- ev[order(ev$date),] ev[aggregate(1:nrow(ev), list(ev$defectnum), max),]
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?
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
>