1

有人可以解释一下下面的行为吗,如果起始日期为“20060301”,则生成的月份序列的第一个元素是 2006 年 3 月,但起始日期为“20060401”,开始日期仍然是 2006 年 3 月?

> 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"

顺便说一句,我知道一个人确实会在一天后开始得到“正确”的答案:

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

编辑:这是我的 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
4

1 回答 1

2

这是一个时区问题,所以设置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"
于 2012-06-23T19:16:14.620 回答