When diff() is applied to POSIXct datetimes, one gets unexpected result. The unit of the differences is not always the same.
On hourly increment POSIXct datetimes, diff() works as expected. If the hours are continuous, diff gives you the difference in hour, as seen below.
beg = ISOdatetime(2016, 11, 6, 1, 0 ,0, tz="Americ/Los_Angeles")
end = ISOdatetime(2016, 11, 7, 23, 0 ,0, tz="Americ/Los_Angeles")
dte = seq(from=beg, to=end, by="hour")
del = diff(dte)
table(del)
del
1
46
If there are gaps, the result is still in hour, which makes sense.
dte = dte[-4]
del = diff(dte)
table(del)
del
1 2
44 1
Now, here is the interesting behavior.
dte1 = sort(c(dte, dte[10]))
del = diff(dte1)
table(del)
del
0 3600 7200
1 44 1
Here I added a duplicate hour, and all of the sudden, the diff unit is now in second.
Is this a bug?
There is a units<-
function for difftime objects:
> units(del) <- 'hours'
> table(del)
del
0 1
1 46
The ?difftime
help page says:
If units = "auto", a suitable set of units is chosen, the largest possible (excluding "weeks") in which all the absolute differences are greater than one.
So perhaps the logic of the function got sidetracked by the 0 value in your case and the units got set to seconds.