diff --git a/NEWS b/NEWS index 7027bf59..dcf6a59c 100644 --- a/NEWS +++ b/NEWS @@ -1,3 +1,8 @@ +Changed in xts 0.11-2.1: + +o endpoints() now honors k > 0 when on = "quarters". Thanks to @alkment for + the report (#279). + Changed in xts 0.11-2: o The to.period() family of functions now use the index timezone when diff --git a/R/endpoints.R b/R/endpoints.R index 71ae9e0e..00a94e98 100644 --- a/R/endpoints.R +++ b/R/endpoints.R @@ -46,8 +46,12 @@ function(x,on='months',k=1) { as.integer(c(0, which(diff(posixltindex$year %/% k + 1) != 0), NR)) }, "quarters" = { - ixqtr <- posixltindex$year * 100L + 190000L + posixltindex$mon %/% 3 + 1 - as.integer(c(0,which(diff(ixqtr) != 0),NR)) + ixyear <- posixltindex$year * 100L + 190000L + ixqtr <- ixyear + posixltindex$mon %/% 3L + 1L + ep <- c(0L, which(diff(ixqtr) != 0L), NR) + if(k > 1) + ep[seq(1,length(ep),k)] + else ep }, "months" = { ixmon <- posixltindex$year * 100L + 190000L + posixltindex$mon diff --git a/inst/unitTests/runit.endpoints.R b/inst/unitTests/runit.endpoints.R index 8c72974d..39b23c9f 100644 --- a/inst/unitTests/runit.endpoints.R +++ b/inst/unitTests/runit.endpoints.R @@ -216,3 +216,14 @@ test.sub_second_resolution_representation <- function() { ep <- endpoints(x, "ms", 200) checkIdentical(ep, seq(0L, 10L, 2L)) } + +# on = "quarters", k > 1 +test.multiple_quarters <- function() { + x <- xts(1:48, as.yearmon("2015-01-01") + 0:47 / 12) + checkIdentical(endpoints(x, "quarters", 1), seq(0L, 48L, 3L)) + checkIdentical(endpoints(x, "quarters", 2), seq(0L, 48L, 6L)) + checkIdentical(endpoints(x, "quarters", 3), seq(0L, 48L, 9L)) + checkIdentical(endpoints(x, "quarters", 4), seq(0L, 48L,12L)) + checkIdentical(endpoints(x, "quarters", 5), seq(0L, 48L,15L)) + checkIdentical(endpoints(x, "quarters", 6), seq(0L, 48L,18L)) +}