[R] date arithmetic discrepancy

Berend Hasselman bhh at xs4all.nl
Sat Jan 28 07:25:31 CET 2012


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

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



More information about the R-help mailing list