Home > Mobile >  R - weird result from difftime function
R - weird result from difftime function

Time:11-02

I was using the difftime function from the base package in R and based on my data I found a couple of weird return values of this function:

> difftime("2014-10-29", "2014-10-21", units = "days")
Time difference of 8.041667 days
> difftime("2020-4-04", "2020-3-28", units = "days")
Time difference of 6.958333 days

Any idea why those values are not integers? Thanks!

All I see in the doc, relevant to it is: "Note that units = "days" means a period of 24 hours, hence takes no account of Daylight Savings Time. Differences in objects of class "Date" are computed as if in the UTC time zone."

CodePudding user response:

I think you should use as.Date to wrap your date strings, e.g.,

> difftime(as.Date("2014-10-29"), as.Date("2014-10-21"), units = "days")
Time difference of 8 days

> difftime(as.Date("2020-4-04"), as.Date("2020-3-28"), units = "days")
Time difference of 7 days

You can observe the difference with or without as.Date

> (a1 <- as.POSIXct("2014-10-29"))
[1] "2014-10-29 CET"

> (a2 <- as.POSIXct("2014-10-21"))
[1] "2014-10-21 CEST"

> (b1 <- as.POSIXct(as.Date("2014-10-29")))
[1] "2014-10-29 01:00:00 CET"

> (b2 <- as.POSIXct(as.Date("2014-10-21")))
[1] "2014-10-21 02:00:00 CEST"

> c(a1, b1)
[1] "2014-10-29 00:00:00 CET" "2014-10-29 01:00:00 CET"

> c(a2, b2)
[1] "2014-10-21 00:00:00 CEST" "2014-10-21 02:00:00 CEST"

CodePudding user response:

The difftime-function uses as.POSIXct() not as.Date() to convert strings to dates, and this includes the system-specific time-zone (if not otherwise provided). Those pairs of dates contain the change to and from summertime in many time-zones, which may be why the time interval is not an integer.

  • Related