[R] difftime result for days not an integer?
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 Would you just round off? Best, Jon __ R-help@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.
Re: [R] difftime result for days not an integer?
On Thu, Feb 11, 2010 at 8:40 PM, Jonathan jonsle...@gmail.com 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 Would you just round off? It's one hour short of an integer number of days: difftime('2004-08-05','2001-01-03',units='hours')/24 Time difference of 1309.958 hours (1+difftime('2004-08-05','2001-01-03',units='hours'))/24 Time difference of 1310 hours why do you think that might be? here's another hint: a few years ago my birthday only lasted 23 hours. I never got that hour back. Barry -- blog: http://geospaced.blogspot.com/ web: http://www.maths.lancs.ac.uk/~rowlings web: http://www.rowlingson.com/ twitter: http://twitter.com/geospacedman pics: http://www.flickr.com/photos/spacedman __ R-help@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.
Re: [R] difftime result for days not an integer?
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 tlum...@u.washington.eduUniversity of Washington, Seattle __ R-help@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.
Re: [R] difftime result for days not an integer?
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 tlum...@u.washington.eduUniversity of Washington, Seattle __ R-help@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 __ R-help@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.