Re: [R] Problem with diff(strptime(...

From: Jim Lemon <>
Date: Fri, 21 Mar 2008 22:02:11 +1100

I think I have worked out the problem, and because it may trouble others, I take the liberty of explaining it on the mailing list.

When diff is applied to a vector of POSIXt values returned by strptime, the units depend upon the smallest interval in the input vector. If that interval is less than one day, _all_ of the differences are in seconds. If the smallest interval is at least one day, all of the differences are in days. This is quite sensible behavior, and I assume it is the "clue" that Prof. Ripley mentioned. However, if the "units" argument is included in the diff call, it has no effect on diff.POSIXt, which I think does the calculation (in contrast, difftime does return 0 days with units="days").

There may be quite a few R users like me who set up their script using a toy dataset and are puzzled when the real dataset produces what looks like garbage. Thus I humbly suggest the following additions to the help file.

When used with times, diff may return different units depending upon the type of time object. An object created by as.Date always returns days, while a POSIXt object will return days if all differences are at least one day, seconds if any are less than one day.

I would also suggest a fix for the underlying code including a "units" argument for diff, except that I could not find it, despite grepping for "diff" in the src directories.

Jim mailing list PLEASE do read the posting guide and provide commented, minimal, self-contained, reproducible code. Received on Fri 21 Mar 2008 - 10:56:41 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Fri 21 Mar 2008 - 13:30:22 GMT.

Mailing list information is available at Please read the posting guide before posting to the list.

list of date sections of archive