tidyquant
tidyquant copied to clipboard
tq_transmute_xy window not being applied to y
Hi, I'm trying to use tq_transmute_xy with a window size of 5. I'm getting a vector length 5 being passed to my function for x but a vector the length of my entire dataframe passed for y.
# data
df <- tibble(p1 = 1:20, p2 = 20:1, date = as.Date(paste(p1, 1, 1, sep = "-")))
# custom function for rollapply
rolling_fun <- function(x = x, y = y, na.rm = TRUE) {
# check the vectors being passed
cat(x)
cat("\n")
cat(y)
stats::weighted.mean(x = x, w = y, na.rm = na.rm)
}
# calculate rolling windows
tq <- tq_transmute_xy(data = df, x = p1, y = p2, mutate_fun = rollapply, width = 5, FUN = rolling_fun)
1 2 3 4 5 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 Error in weighted.mean.default(x = x, w = y, na.rm = na.rm) : 'x' and 'w' must have the same length
> sessionInfo()
R version 3.6.2 (2019-12-12)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 17763)
Matrix products: default
locale:
[1] LC_COLLATE=English_Australia.1252 LC_CTYPE=English_Australia.1252 LC_MONETARY=English_Australia.1252
[4] LC_NUMERIC=C LC_TIME=English_Australia.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] tidyquant_0.5.10 quantmod_0.4-15 TTR_0.23-6 PerformanceAnalytics_2.0.4
[5] xts_0.12-0 zoo_1.8-7 lubridate_1.7.4 magrittr_1.5
[9] forcats_0.4.0 stringr_1.4.0 dplyr_0.8.4 purrr_0.3.3
[13] readr_1.3.1 tidyr_1.0.2 tibble_2.1.3 ggplot2_3.2.1
[17] tidyverse_1.3.0
loaded via a namespace (and not attached):
[1] Rcpp_1.0.3 lattice_0.20-38 prettyunits_1.1.1 ps_1.3.0 assertthat_0.2.1 rprojroot_1.3-2 digest_0.6.23
[8] packrat_0.5.0 R6_2.4.1 cellranger_1.1.0 backports_1.1.5 reprex_0.3.0 httr_1.4.1 pillar_1.4.3
[15] rlang_0.4.4 lazyeval_0.2.2 curl_4.3 readxl_1.3.1 rstudioapi_0.11 callr_3.4.1 desc_1.2.0
[22] devtools_2.2.1 timetk_0.1.2 munsell_0.5.0 broom_0.5.4 compiler_3.6.2 modelr_0.1.5 pkgconfig_2.0.3
[29] pkgbuild_1.0.6 tidyselect_1.0.0 quadprog_1.5-8 fansi_0.4.1 crayon_1.3.4 dbplyr_1.4.2 withr_2.1.2
[36] Quandl_2.10.0 grid_3.6.2 nlme_3.1-142 jsonlite_1.6.1 gtable_0.3.0 lifecycle_0.1.0 DBI_1.1.0
[43] scales_1.1.0 cli_2.0.1 stringi_1.4.4 fs_1.3.1 remotes_2.1.0 testthat_2.3.1 xml2_1.2.2
[50] ellipsis_0.3.0 vctrs_0.2.2 generics_0.0.2 tools_3.6.2 glue_1.3.1 hms_0.5.3 processx_3.4.2
[57] pkgload_1.0.2 colorspace_1.4-1 sessioninfo_1.1.1 rvest_0.3.5 memoise_1.1.0 haven_2.2.0 usethis_1.5.1