Fix split.xts behavior before era (1-1-1970)

I noticed some strange xts behavior when trying to split an object that goes a long way. The behavior of split changes in the era.

#Create some data dates <- seq(as.Date("1960-01-01"),as.Date("1980-01-01"),"days") x <- rnorm(length(dates)) data <- xts(x, order.by=dates) 

If we divide the xts object by the week, it will define the last day of the week as Monday until 1970. After 1970, he defines it as Sunday (expected behavior).

 #Split the data, keep the last day of the week lastdayofweek <- do.call(rbind, lapply(split(data, "weeks"), last)) head(lastdayofweek) tail(lastdayofweek) 

Calendar 1960

Calendar 1979

This seems like a problem only for weeks, not months or years.

 #Split the data, keep the last day of the month lastdayofmonth <- do.call(rbind, lapply(split(data, "months"), last)) head(lastdayofmonth) tail(lastdayofmonth) 

The behavior is likely to be related to the following, although I'm not sure why it will only apply to weeks. From xts cran .

For dates before the era (1970-01-01), the end time is 59.0000 seconds. This is due to an error / function in the implementation of R asPOSIXct and mktime0 at the C-source level. This limits the accuracy of ranges to 1970 to 1 minute of granularity with the current time traversal of xts.

My workaround was to shift the dates before dividing the objects into data until 1970, if I was dividing into weeks. I expect someone else to have a more elegant solution (or a way to avoid a mistake).

EDIT: To be clear what the question is, I am looking for an answer that a) indicates why this happens (so I can better understand the nature of the error and therefore avoid it) and / or b) the best way to solve the problem.

+4
source share
1 answer

One workaround is to check Rev. 743 or earlier, as it seems to me that this violated Rev. 744.

 svn checkout svn://svn.r-forge.r-project.org/svnroot/xts/@743 

But the idea is much to write a bug report so you don't have to use the old version forever. (also, of course, other errors can be fixed and / or new functions added after Rev. 743)

+4
source

All Articles