joshuaulrich / xts

Extensible time series class that provides uniform handling of many R time series classes by extending zoo.
http://joshuaulrich.github.io/xts/
GNU General Public License v2.0
220 stars 71 forks source link

endpoints ignore k when using quarters #279

Closed alkment closed 5 years ago

alkment commented 5 years ago

Description

endpoints ignores k when using on='quarters'

Expected behavior

Expecting to get the index of the endpoint of every second quarter. For example getting 0,181,365 as output from the example below.

Minimal, reproducible example

endpoints(xts(1:365,as.Date('2018-01-01')+(0:364)),'quarters',1)
[1]   0  90 181 273 365
endpoints(xts(1:365,as.Date('2018-01-01')+(0:364)),'quarters',2)
[1]   0  90 181 273 365

Possible Solution

changing the switch case to something like this might work

quarters = {
        ixqtr <- posixltindex$year * 100L + 190000L + posixltindex$mon%/%3 + 
            1
        ans=as.integer(c(0, which(diff(ixqtr) != 0), NR))
        ans[unique(c(1,1+seq_len((length(ans)-1)/k)*k,length(ans)))]
    }

Session Info

R version 3.4.4 (2018-03-15)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 16.04.5 LTS

Matrix products: default
BLAS: /usr/lib/openblas-base/libblas.so.3
LAPACK: /usr/lib/libopenblasp-r0.2.18.so

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C               LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8     LC_MONETARY=en_US.UTF-8   
 [6] LC_MESSAGES=en_US.UTF-8    LC_PAPER=en_US.UTF-8       LC_NAME=C                  LC_ADDRESS=C               LC_TELEPHONE=C            
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] xts_0.11-2 zoo_1.8-4 

loaded via a namespace (and not attached):
[1] compiler_3.4.4  tools_3.4.4     yaml_2.2.0      grid_3.4.4      lattice_0.20-35
joshuaulrich commented 5 years ago

Thanks for the report and suggested fix! I decided to use the same pattern as when k > 1 and on = "months". Thanks again for your contribution!