[R] diff, POSIXct, POSIXlt, POSIXt

From: Patrick Giraudoux <patrick.giraudoux_at_univ-fcomte.fr>
Date: Sun 23 Jul 2006 - 17:17:10 EST


Dear Listers,

I have encountered a strange problem using diff() and POSIXt:

dts<-c("15/4/2003","15/7/2003","15/10/2003","15/04/2004","15/07/2004","15/10/2004","15/4/2005","15/07/2005","15/10/2005","15/4/2006") dts <- strptime(dts, "%d/%m/%Y")
class(dts)

[1] "POSIXt" "POSIXlt"

diff(dts)

Time differences of 7862400, 7948800, 15811200, 7862400, 7948800, 15724800, 7862400, 7948800, 0 secs

In this case the result is not the one expected: expressed in seconds and not in days, and the difference between the two last dates is not 0.

Now, if one use a vector of 9 dates only (whatever the date removed), things come well:

diff(dts[-1])

Time differences of 92, 183, 91, 92, 182, 91, 92, 182 days

Also if one contrains dts to POSIXct

dts<-c("15/4/2003","15/7/2003","15/10/2003","15/04/2004","15/07/2004","15/10/2004","15/4/2005","15/07/2005","15/10/2005","15/4/2006") dts <- as.POSIXct(strptime(dts, "%d/%m/%Y")) diff(dts)

Time differences of 91, 92, 183, 91, 92, 182, 91, 92, 182 days

Any rational in that?

Patrick



R-help@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 and provide commented, minimal, self-contained, reproducible code. Received on Sun Jul 23 17:19:58 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Sun 23 Jul 2006 - 22:17:18 EST.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.