[R] date arithmetic discrepancy
peter dalgaard
pdalgd at gmail.com
Sat Jan 28 09:41:06 CET 2012
On Jan 28, 2012, at 07:25 , Berend Hasselman wrote:
>
> On 27-01-2012, at 21:45, Ty Canuck wrote:
>
>> What accounts for the different result?
>>
>>> as.numeric(as.POSIXct("2012-01-13 08:10:00") - as.POSIXct("2012-01-13
>> 00:00:00") , units='secs')
>> [1] 29400
>>
>>> trunc(as.numeric(as.POSIXct("2012-01-13 08:10:00") -
>> as.POSIXct("2012-01-13 00:00:00") , units='secs') )
>> [1] 29399
>
> See R FAQ 7.31
More specifically, the "auto" units of difftime() (implied by the subtraction) converts to hours and 29400/3600 cannot be represented exactly in binary.
To retain exact calculations in seconds, set the units directly.
> difftime(as.POSIXct("2012-01-13 08:10:00"), as.POSIXct("2012-01-13 00:00:00"),units="secs")
Time difference of 29400 secs
>
> And try this
>
> d1 <- as.numeric(as.POSIXct("2012-01-13 08:10:00") - as.POSIXct("2012-01-13 00:00:00") , units='secs')
> d2 <- trunc(as.numeric(as.POSIXct("2012-01-13 08:10:00") - as.POSIXct("2012-01-13 00:00:00") , units='secs') )
>
> sprintf("%.12f",d1)
>
> Berend
>
> ______________________________________________
> 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.
--
Peter Dalgaard, Professor,
Center for Statistics, Copenhagen Business School
Solbjerg Plads 3, 2000 Frederiksberg, Denmark
Phone: (+45)38153501
Email: pd.mes at cbs.dk Priv: PDalgd at gmail.com
More information about the R-help
mailing list