[R] difftime

Gabor Grothendieck ggrothendieck at gmail.com
Tue Dec 9 20:33:01 CET 2008


I guess its the same problem as this (run after your
code):

> as.POSIXct(a, tz = "")
[1] NA
> as.POSIXct(b, tz = "")
[1] NA
> difftime(b, a, units="days")
Time difference of NA days

If you explicitly specify the tz as "GMT" then it works as expected:

> as.POSIXct(a, tz = "GMT")
[1] "1999-10-08 07:00:00 GMT"
> as.POSIXct(b, tz = "GMT")
[1] "1999-10-27 18:00:00 GMT"
> difftime(b, a, tz = "GMT", units="days")
Time difference of 19.45833 days

Since the problem is confusion between the "" and "GMT"
time zones the easiest thing to do is to simply set the
default time zone for the remainder of the session to "GMT"

Sys.setenv(TZ = "GMT")

in which case tz = "" and tz = "GMT" are the same and
you should get the expected answer either way.  Alternately
use chron which does not have time zones in the first place
so problem cannot arise.

See R News 4/1.

The above was done under:
> R.version.string # Vista
[1] "R version 2.8.0 Patched (2008-11-10 r46884)"


On Tue, Dec 9, 2008 at 12:33 PM, eric lee <ericlee100 at gmail.com> wrote:
> Hi.  I'm trying to take the difference in days between two times.  Can
> you point out what's wrong, or suggest a different function?  When I
> try the following code,  The following code works fine:
>
> a <- strptime(1911100807,format="%Y%m%d%H",tz="GMT")
> b <- strptime(1911102718,format="%Y%m%d%H",tz="GMT")
> x <- difftime(b, a, units="days")
> x
>
>
> But when I change the year, the following code returns 'NA' for the
> time between a and b.  Thanks.
>
> a <- strptime(1999100807,format="%Y%m%d%H",tz="GMT")
> b <- strptime(1999102718,format="%Y%m%d%H",tz="GMT")
> x <- difftime(b, a, units="days")
> x
>
> ______________________________________________
> 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