[R] Difference between two datetimes

David Fox david.fox at environmetrics.net.au
Wed Jan 29 02:15:14 CET 2014


I have a data frame with variable "datetime" which is of class POSIXct.
Consecutive observations are separated by 30 minutes.
However, some of the differences reported by R give unexpected results.
For example consider the following two consecutive entries:

> par.dat$datetime[5944]
[1] "2010-04-04 02:30:00 EST"

> par.dat$datetime[5945]
[1] "2010-04-04 03:00:00 EST"

When I examine the difference, R reports 1.5 hours instead of 30 minutes:

> par.dat$datetime[5945]-par.dat$datetime[5944]
Time difference of 1.5 hours

On further investigation it appears there's something peculiar to this
particular date. Other years work fine, eg:
> as.POSIXct("2011-04-04 03:00:00")- as.POSIXct("2011-04-04 02:30:00")
Time difference of 30 mins

> as.POSIXct("2012-04-04 03:00:00")- as.POSIXct("2012-04-04 02:30:00")
Time difference of 30 mins

> as.POSIXct("2009-04-04 03:00:00")- as.POSIXct("2009-04-04 02:30:00")
Time difference of 30 mins

But when I use 2010 I get a difference of 1.5 hours:

> as.POSIXct("2010-04-04 03:00:00")- as.POSIXct("2010-04-04 02:30:00")
Time difference of 1.5 hours






Any suggestions?
Thanks,
David Fox.



More information about the R-help mailing list