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
Perhaps you are getting warning messages from difftime like this: "Warning messages: 1: In structure(.Internal(as.POSIXct(x, tz)), class = c("POSIXt", "POSIXct"), : unable to identify current timezone 'V': please set environment variable 'TZ' 2: In structure(.Internal(as.POSIXct(x, tz)), class = c("POSIXt", "POSIXct"), : unknwon timezone 'localtime'" Try execute the code two times. On Tue, Dec 9, 2008 at 3:33 PM, eric lee <ericlee100@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@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. >-- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40" S 49° 16' 22" O [[alternative HTML version deleted]]
In the absence of any of the information requested in the posting guide, all we can say is that this works for other people (e.g. on both Mac OS X and Fedora 8 for me). Apart from the usual info, we would need to know your timezone. On Tue, 9 Dec 2008, eric lee 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.PLEASE do. -- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595
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. >