【发布时间】:2018-09-22 19:54:00
【问题描述】:
我正在尝试计算 tibble 上的滚动相关性,在循环中遍历列名。不过,我似乎在努力将变量传递给函数。这有效:
tbl <- tibble(date = seq(as.Date("1983-03-31"), by=7, length.out=100),
col1 = 1:100, col2 = sample(100, size = 100, replace=TRUE), col3 = col1 + col2)
tbl %>%
tq_mutate_xy(
x = col1,
y = col3,
mutate_fun = runCor,
n = 10,
use = "pairwise.complete.obs",
col_rename = "col1_col3_corr"
)
但这不是:
tbl <- tibble(date = seq(as.Date("1983-03-31"), by=7, length.out=100),
col1 = 1:100, col2 = sample(100, size = 100, replace=TRUE), col3 = col1 + col2)
c1 <- "col1"
c2 <- "col3"
tbl %>%
tq_mutate_xy(
x = !!c1,
y = !!c2,
mutate_fun = runCor,
n = 10,
use = "pairwise.complete.obs",
col_rename = paste0(c1, "_", c2, "_corr")
)
错误是“check_x_y_valid(data, x, y) 中的错误:x = !(!c1) 不是有效名称。”
我做错了什么?
【问题讨论】:
-
如果这是
tq_mutate_xy的来源,您可能需要用[tidyquant]标记它 -
您可能必须使用
!!rlang::sym(c1)而不是!!c1。这个网站上有很多例子。 -
@mt1022 check_x_y_valid(data, x, y) 出错:x = !(!rlang::sym(c1)) 名称无效。
-
@arvi1000 完成。虽然如果有更直接的计算滚动相关性的方法,我愿意接受建议。
-
你可以使用
!!rlang::as_quosure(as.name(col1))而不是!!col1这应该可以