Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

endpoints fails on sparse monthly data #169

Closed
joshuaulrich opened this issue Mar 10, 2017 · 0 comments
Closed

endpoints fails on sparse monthly data #169

joshuaulrich opened this issue Mar 10, 2017 · 0 comments
Labels

Comments

@joshuaulrich
Copy link
Owner

Since endpoints() only uses POSIXlt$mon to determine endpoints, it misses the case when the data is sparse and there is a year between observations. For example:

(x <- xts(1:6, as.Date(sprintf("199%d-06-%02d", c(2, 2, 2, 3:5), 21:26))))
#            [,1]
# 1992-06-21    1
# 1992-06-22    2
# 1992-06-23    3
# 1993-06-24    4
# 1994-06-25    5
# 1995-06-26    6
endpoints(x, "months", 1)
# [1] 0 6

When the correct result is:

endpointsMonthHack <- function(x, on = "months", k = 1) {
  # yearmon index
  ymIndex <- as.yearmon(index(x))
  # month change locations
  locations <- which(diff(ymIndex) != 0)
  # endpoints
  ep <- c(0, locations, nrow(x))
  unique(ep)
}
period.apply(x, endpointsMonthHack(x), mean)
#            [,1]
# 1992-06-23    2
# 1993-06-24    4
# 1994-06-25    5
# 1995-06-26    6

Thanks to Scott Waichler for the helpful report.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant