1

Can some please explain the behaviour below, where, with a from date of "20060301", the first element of the resulting sequence of months is March 2006, but with a from date of "20060401", the start date remains March 2006?

> timeBasedSeq('20060301/20120207/m')[1]
[1] "Mar 2006"
> timeBasedSeq('20060401/20120207/m')[1]
[1] "Mar 2006"
> timeBasedSeq('20060501/20120207/m')[1]
[1] "Apr 2006"
> timeBasedSeq('20060601/20120207/m')[1]
[1] "May 2006"
> timeBasedSeq('20060701/20120207/m')[1]
[1] "Jun 2006"
> timeBasedSeq('20060801/20120207/m')[1]
[1] "Jul 2006"

BTW, I am aware that one does get the "right" answer by starting a day later:

> timeBasedSeq('20060401/20120207/m')[1]
[1] "Mar 2006"
> timeBasedSeq('20060402/20120207/m')[1]
[1] "Apr 2006"

EDIT: Here is my sessionInfo()

> sessionInfo()
R version 2.15.0 (2012-03-30)
Platform: x86_64-pc-linux-gnu (64-bit)

locale:
[1] LC_CTYPE=en_GB.UTF-8       LC_NUMERIC=C              
[3] LC_TIME=en_GB.UTF-8        LC_COLLATE=en_GB.UTF-8    
[5] LC_MONETARY=en_GB.UTF-8    LC_MESSAGES=en_GB.UTF-8   
[7] LC_PAPER=C                 LC_NAME=C                 
[9] LC_ADDRESS=C               LC_TELEPHONE=C            
[11] LC_MEASUREMENT=en_GB.UTF-8 LC_IDENTIFICATION=C       

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] xts_0.8-6 zoo_1.7-7

loaded via a namespace (and not attached):
[1] grid_2.15.0    lattice_0.20-6 tcltk_2.15.0   tools_2.15.0
Rahul Savani
  • 872
  • 1
  • 10
  • 24

1 Answers1

2

This is a timezone issue, so set Sys.setenv(TZ="GMT").

R> Sys.setenv(TZ="Europe/London"); Sys.time()
[1] "2012-06-23 20:14:58 BST"
R> timeBasedSeq('200603/201202/m')[1]
[1] "Mar 2006"
R> timeBasedSeq('200604/201202/m')[1]
[1] "Mar 2006"
R> Sys.setenv(TZ="GMT"); Sys.time()
[1] "2012-06-23 19:15:49 GMT"
R> timeBasedSeq('200603/201202/m')[1]
[1] "Mar 2006"
R> timeBasedSeq('200604/201202/m')[1]
[1] "Apr 2006"
Joshua Ulrich
  • 173,410
  • 32
  • 338
  • 418
  • Great, thanks. So what's happening in the first case is that 20060401 is interpreted as 20060401 00:00 BST, and converted to 20060331 23:00 GMT? – Rahul Savani Jun 23 '12 at 20:09
  • The actual problem is `zoo:::as.yearmon.POSIXt`, which has GMT hard-coded. If you want `yearmon`, set `retclass="Date"`, then convert the result to `yearmon` yourself: `as.yearmon(timeBasedSeq('200604/201202/m',retclass="Date")[1])`. – Joshua Ulrich Jun 23 '12 at 22:14