Skip to content

Commit

Permalink
Honor k > 1 when on = "quarters"
Browse files Browse the repository at this point in the history
endpoints() completely ignored the 'k' argument when on = "quarters".
Use the same logic for k > 1 when on = "months": calculate endpoints
for k = 1, then subset the result from 1 to length(endpoints) by k.

Fixes #279.
  • Loading branch information
joshuaulrich committed Nov 18, 2018
1 parent 1a35d79 commit 138d80c
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
5 changes: 5 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
@@ -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
Expand Down
8 changes: 6 additions & 2 deletions R/endpoints.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions inst/unitTests/runit.endpoints.R
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}

0 comments on commit 138d80c

Please sign in to comment.