timeBasedSeq function repeats some of the days it creates!

狂风中的少年 提交于 2019-12-01 19:21:20
42-

The bug is in xts. That function uses seq.POSIXct, and the same behavior can be produced by:

seq(as.POSIXct("1986-10-01"), as.POSIXct("1986-11-01"), by="day")

And even more surprising to me by seq.POSIXlt

seq(as.POSIXlt("1986-10-01"), as.POSIXlt("1986-11-01"), by="day")

But that behavior is well documented in seq.POSIXt and there is a provision for using by="DSTday" which the xts authors should probably have used for the situation when days are the implicit interval. The temporary workaround is:

timeBasedSeq("19860601/19861231")[ !duplicated(timeBasedSeq("19860601/19861231") ]

or more compactly:

unique(timeBasedSeq("19860601/19861231"))

To expand my comment: The bug is explained by the change from daylight savings time. At this point adding 24 hours does not move you to the next day since an hour is taken off.

> as.POSIXct("1986-10-26")
[1] "1986-10-26 BST"
> as.POSIXct("1986-10-26")+24*60*60
[1] "1986-10-26 23:00:00 GMT"

One solution would be to base the date around midday rather than midnight and use strftime to strip out extraneous information and as.POSIXlt to convert back to a time class:

as.POSIXlt(strftime(timeBasedSeq("19860601 1200/19861231/d"),"%Y-%m-%d"))

...
[146] "1986-10-24" "1986-10-25" "1986-10-26" "1986-10-27" "1986-10-28"
...
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!