I am using The timeBasedSeq function from xts package to use as index in a time series/zoo object but it repeats some of the days it creates! Which than causes problems with zoo because the "index entries in ‘order.by’ are not unique".
For example:
timeBasedSeq("19860601/19861231")
will create
..."1986-10-23" "1986-10-24" "1986-10-25" "1986-10-26" "1986-10-26" "1986-10-27" "1986-10-28" "1986-10-29""...
So for some reason it repeats the 26th, how can I avoid that?
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"
...
来源:https://stackoverflow.com/questions/6074300/timebasedseq-function-repeats-some-of-the-days-it-creates