The timeBasedSeq function repeats some of the days that it creates!

I use the TimeBasedSeq function from the xts package to use as the index in the / zoo time series object, but it repeats some of the days it creates! Which causes problems with the zoo because "the index entries in" order.by "are not unique."

For instance:

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, he repeats the 26th, how can I avoid this?

+4
source share
2 answers

Error in xts. This function uses seq.POSIXct , and the same behavior can be caused by:

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

And even more surprising for me is seq.POSIXlt

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

But this behavior is well documented in seq.POSIXt, and there is a condition for using by="DSTday" , which xts authors should probably have used for situations where days are an implicit interval. Temporary solution:

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

or more compact:

 unique(timeBasedSeq("19860601/19861231")) 
+4
source

To expand my comment: the error is due to a change from daylight saving time. At this point, adding 24 hours does not transfer you to the day after the hour is removed.

 > 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 at about noon, not midnight, and use strftime to extract 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" ... 
+1
source

All Articles