卷曲卷曲无法识别引用的数据
Curly curly not recognizing quoted data
我想使用 purrr:map2
将两个向量输入到我的自定义函数中。不幸的是,当我这样做时,出现错误 Error: Problem with `filter()` input `..2`. x Input `..2` must be of size 93 or 1, not size 9. ℹ Input `..2` is `!is.na(y)`.
如何让我的自定义函数使用参数作为数据并运行成功使用 map2?
这是一个 MWE:
library(MASS)
data(Cars93)
crosstab_3way_custom <- function(y, z) {
pollster::crosstab_3way(Cars93,
x = Origin,
y= {{ y }},
z = {{ z }},
weight = rep(1, NROW(Cars93)),
pct_type = "row")
}
combos <- expand.grid(c("Manufacturer", "Model", "Type"), c("Min.Price", "Price", "Max.Price"))
combos <- combos %>% map(as.character)
map2(combos[1], combos[2], function(y,z) crosstab_3way_custom(y,z))
要传递带引号的变量,请使用 sym
和 !!
:
crosstab_3way_custom <- function(y, z) {
pollster::crosstab_3way(data,
x = Origin,
y = !!sym(y),
z = !!sym(z),
weight = 1,
pct_type = "row")
}
purrr::map2(combos$Var1, combos$Var2, crosstab_3way_custom)
我想使用 purrr:map2
将两个向量输入到我的自定义函数中。不幸的是,当我这样做时,出现错误 Error: Problem with `filter()` input `..2`. x Input `..2` must be of size 93 or 1, not size 9. ℹ Input `..2` is `!is.na(y)`.
如何让我的自定义函数使用参数作为数据并运行成功使用 map2?
这是一个 MWE:
library(MASS)
data(Cars93)
crosstab_3way_custom <- function(y, z) {
pollster::crosstab_3way(Cars93,
x = Origin,
y= {{ y }},
z = {{ z }},
weight = rep(1, NROW(Cars93)),
pct_type = "row")
}
combos <- expand.grid(c("Manufacturer", "Model", "Type"), c("Min.Price", "Price", "Max.Price"))
combos <- combos %>% map(as.character)
map2(combos[1], combos[2], function(y,z) crosstab_3way_custom(y,z))
要传递带引号的变量,请使用 sym
和 !!
:
crosstab_3way_custom <- function(y, z) {
pollster::crosstab_3way(data,
x = Origin,
y = !!sym(y),
z = !!sym(z),
weight = 1,
pct_type = "row")
}
purrr::map2(combos$Var1, combos$Var2, crosstab_3way_custom)