seq.POSIXt() with short time intervals
On Tue, 26 Mar 2002, Don MacQueen wrote:
I was surprised when seq.POSIXt() returned a single value rather than a vector, from inputs that I thought were reasonable. Here's an example to illustrate:
t0 <- ISOdatetime(2002,6,24,0,0,10)
## expected a sequence of 16 times 1 second apart
seq.POSIXt(from=t0,to=t0+15,by='1 sec')
[1] "2002-06-24 00:00:10 PDT" ## traces to this call
seq.default(1024902010,1024902025,by=1)
[1] 1024902010
## which is due to the following expressions in seq.default()
del <- to - from
## a few lines omitted
dd <- abs(del)/max(abs(to), abs(from))
if (dd < sqrt(.Machine$double.eps)) return(from)
This looks like a trap to catch bad input, and return something
reasonable (or at least not crash).
It's a given that seq.default() pre-dates seq.POSIXt() by many years,
and the test for relative range undoubtedly has good reasons, so it's
probably not a good idea to change seq.default().
Oh, I think it needs to be changed. That is far too generous a test for numerical inaccuracies, by say 5 orders of magnitude. It is also undocumented, so I'd post a bug report on seq.default.
There's nothing inherently wrong with such a time sequence, and in fact, if t0 = ISOdatetime(1970,6,24,0,0,10) instead, seq.POSIXt() does return a vector. I'd like to see seq.POSIXt() return a full sequence in such cases, regardless of the starting point, so what I'll do is think about it, and see if I can't come up with a good way to modify seq.POSIXt() to do that. In the meantime, I'm interested in any comments or suggestions anyone might have.
Easy: subtract the starting value before the call and add it back on afterwards.
Turns out that with the base time, t0, as above, t0+15 is right at the threshold of the relative range test: ## compare with t0=t0+16 ## this call returns what was expected
seq.POSIXt(from=t0, to=t0+16, by='1 sec')
[1] "2002-06-24 00:00:10 PDT" "2002-06-24 00:00:11 PDT" "2002-06-24 00:00:12 PDT" "2002-06-24 00:00:13 PDT" [5] "2002-06-24 00:00:14 PDT" "2002-06-24 00:00:15 PDT" "2002-06-24 00:00:16 PDT" "2002-06-24 00:00:17 PDT" [9] "2002-06-24 00:00:18 PDT" "2002-06-24 00:00:19 PDT" "2002-06-24 00:00:20 PDT" "2002-06-24 00:00:21 PDT" [13] "2002-06-24 00:00:22 PDT" "2002-06-24 00:00:23 PDT" "2002-06-24 00:00:24 PDT" "2002-06-24 00:00:25 PDT" [17] "2002-06-24 00:00:26 PDT" I've found this on two platforms:
version
_ platform sparc-sun-solaris2.7 arch sparc os solaris2.7 system sparc, solaris2.7 status major 1 minor 4.1 year 2002 month 01 day 30 language R platform powerpc-apple-darwin5.3 arch powerpc os darwin5.3 system powerpc, darwin5.3 status major 1 minor 4.1 (also 4.0) year 2001 month 12 day 19 language R -- -------------------------------------- Don MacQueen Environmental Protection Department Lawrence Livermore National Laboratory Livermore, CA, USA -------------------------------------- -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
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 272860 (secr) Oxford OX1 3TG, UK Fax: +44 1865 272595 -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._