[R] Problem with diff(strptime(...
Jim Lemon
jim at bitwrit.com.au
Fri Mar 21 12:02:11 CET 2008
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.
Value
...
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
More information about the R-help
mailing list