[R] difftime result for days not an integer?
Don MacQueen
macq at llnl.gov
Thu Feb 11 22:46:37 CET 2010
Which brings up another point.
The help page for difftime specifies that it operates on date-time or
date objects. But
'2004-08-05' is neither of these, it is a character object.
At this point, one might ask... I didn't give it what it asked for,
what is it going to do?
(might give me an error message, might do who knows what, but find out!)
R is pretty good about automatic conversions between types, so it's
reasonable that difftime() would convert the arguments to a valid
type, if it can. But which one, since there are two valid types?
And as we have seen, it is to date-time, not date.
For some, but certainly not all, R functions, details like this can
be discovered by typing the name of the function at the R prompt,
without the parentheses.
> difftime
function (time1, time2, tz = "", units = c("auto", "secs", "mins",
"hours", "days", "weeks"))
{
time1 <- as.POSIXct(time1, tz = tz)
time2 <- as.POSIXct(time2, tz = tz)
... etc ...
-Don
At 12:53 PM -0800 2/11/10, Thomas Lumley wrote:
>On Thu, 11 Feb 2010, Jonathan wrote:
>
>>Anybody have an idea why I would get a non-integer value for the
>>number of days here?
>>
>>>difftime('2004-08-05','2001-01-03',units='days')
>>Time difference of 1309.958 days
>
>Because it's not a whole number of 24-hour periods, due to daylight
>saving time: 0.958 is 23/24.
>
>>
>>Would you just round off?
>>
>
>Yes, or use as.Date() if you only want to consider whole days
>
>R> as.Date('2004-08-05')-as.Date('2001-01-03')
>Time difference of 1310 days
>
> -thomas
>
>Thomas Lumley Assoc. Professor, Biostatistics
>tlumley at u.washington.edu University of Washington, Seattle
>
>______________________________________________
>R-help at r-project.org mailing list
>https://*stat.ethz.ch/mailman/listinfo/r-help
>PLEASE do read the posting guide http://*www.*R-project.org/posting-guide.html
>and provide commented, minimal, self-contained, reproducible code.
--
--------------------------------------
Don MacQueen
Environmental Protection Department
Lawrence Livermore National Laboratory
Livermore, CA, USA
925-423-1062
More information about the R-help
mailing list