[R] difftime() out by 1 hour

Pascal Oettli kridox at ymail.com
Fri Feb 1 09:17:38 CET 2013


Hello,

Here is the result I get using your script:

 > sessionInfo()
R version 2.15.2 (2012-10-26)
Platform: x86_64-suse-linux-gnu (64-bit)


[1] "2012-03-31 21:00:00" "2012-04-01 00:00:00" "2012-04-01 03:00:00"
[4] "2012-04-01 06:00:00"
Time differences in hours
[1] 3 3 3
attr(,"tzone")
[1] ""
[1] "2012-10-06 21:00:00" "2012-10-07 00:00:00" "2012-10-07 03:00:00"
[4] "2012-10-07 06:00:00"
Time differences in hours
[1] 3 3 3
attr(,"tzone")
[1] ""

Regards,
Pascal


Le 01/02/2013 16:27, Boots a écrit :
> I have a problem with results  from difftime being 1 hour different than expected. 2 examples are given below:
>
> datetime <- matrix(data=rbind(c("2012-03-31 21:00:00", "2012-04-01 00:00:00", "2012-04-01 03:00:00", "2012-04-01 06:00:00"),
>                     c("2012-10-06 21:00:00", "2012-10-07 00:00:00", "2012-10-07 03:00:00", "2012-10-07 06:00:00")), ncol=4, nrow=2)
>
> for (row in 1:2) {
>    x <- datetime[row,]
>    x1=x[1:length(x)-1]
>    x2=x[2:length(x)]
>    xd=difftime(x2,x1)
>    print (x)
>    print (xd)
> }
>
> [1] "2012-03-31 21:00:00" "2012-04-01 00:00:00" "2012-04-01 03:00:00" "2012-04-01 06:00:00"
> Time differences in hours
> [1] 3 4 3
> attr(,"tzone")
> [1] ""
> [1] "2012-10-06 21:00:00" "2012-10-07 00:00:00" "2012-10-07 03:00:00" "2012-10-07 06:00:00"
> Time differences in hours
> [1] 3 2 3
> attr(,"tzone")
> [1] ""
>
> Accurate results would be 3 3 3 (hours) in both cases.
> The problem occurs randomly, but only around midnight.
> All the dates between "2012-04-01 06:00:00" and "2012-10-06 21:00:00" are fine.
>
> I have seen postings about problems with "POSIXlt" and "POSIXct" accuracy
> because of truncation rather than rounding of floating point representations.
> The response was that it was expected behaviour.
>
> So my question is this expected behaviour, have I used difftime incorrectly, and if not, is there a set of date/time functions and classes that use
> integer representation to give more accurate results? I am new to R and I may well be using the wrong set of functions.
> Garry
>
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> 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.
>



More information about the R-help mailing list