xts
xts copied to clipboard
`endpoints()` returns unexpected values when `on = years` and k > 1
library(xts)
set.seed(1234)
xx <- as.xts(as.matrix(rnorm(341*12)),
seq.Date(as.Date(Sys.Date()),
by = "day",
length.out = 341*12))
head(xx, 2)
#> [,1]
#> 2019-06-01 -1.2070657
#> 2019-06-02 0.2774292
tail(xx, 2)
#> [,1]
#> 2030-08-12 1.7300719
#> 2030-08-13 -0.2922716
# expected starting date 2020-12-31
xx[endpoints(xx, on = "years", k = 2),]
#> [,1]
#> 2019-12-31 -1.6070809
#> 2021-12-31 -0.3770091
#> 2023-12-31 0.7714331
#> 2025-12-31 0.4784294
#> 2027-12-31 -0.0193127
#> 2029-12-31 0.2524384
#> 2030-08-13 -0.2922716
# expected starting date 2021-12-31
xx[endpoints(xx, on = "years", k = 3),]
#> [,1]
#> 2019-12-31 -1.60708094
#> 2022-12-31 0.04152571
#> 2025-12-31 0.47842942
#> 2028-12-31 -0.87802036
#> 2030-08-13 -0.29227156
# Also, passing negative numbers into `k` should return the k = 1 endpoints, but :
xx[endpoints(xx, on = "years", k = -2),]
#> [,1]
#> 2020-12-31 -1.01371766
#> 2022-12-31 0.04152571
#> 2024-12-31 0.35920383
#> 2026-12-31 0.52029913
#> 2028-12-31 -0.87802036
#> 2030-08-13 -0.29227156
xx[endpoints(xx, on = "years", k = -3),]
#> [,1]
#> 2020-12-31 -1.0137177
#> 2023-12-31 0.7714331
#> 2026-12-31 0.5202991
#> 2029-12-31 0.2524384
#> 2030-08-13 -0.2922716
devtools::session_info()
#> - Session info ----------------------------------------------------------
#> setting value
#> version R version 3.6.0 (2019-04-26)
#> os Windows 7 x64 SP 1
#> system x86_64, mingw32
#> ui RTerm
#> language (EN)
#> collate English_United Kingdom.1252
#> ctype English_United Kingdom.1252
#> tz Europe/London
#> date 2019-06-01
#>
#> - Packages --------------------------------------------------------------
#> package * version date lib source
#> xts * 0.12-1 2019-06-01 [1] Github (joshuaulrich/xts@efe399f)
#> zoo * 1.8-6 2019-05-28 [1] CRAN (R 3.6.0)
#>
#> [1] C:/Program Files/R/library
#> [2] C:/Program Files/R/R-3.6.0/library
I'm not sure this is a bug. endpoints()
is based on calculations from the Unix origin (midnight 1970-01-01). That said, I can understand how this is unexpected behavior.
Oh, I'd also appreciate it if you could omit the advertisement for the reprex package from your reports. Sorry to be picky, but I find it very annoying.
You're right it's annoying, I missed it.
I'm going to resolve this by updating the documentation to clarify how endpoints()
calculates the return values based on periods from the origin (midnight 1970-01-01).
Also, note that setting k < 1
is now an error.
Related to #153.