将 enquo 与 infer 包一起使用
Using enquo with infer package
我正在使用推断包进行 运行 卡方检验,例如,
df %>%
chisq_test(label ~ feature)
我想把它放到一个函数中,这样我就可以写:
my_chisq_function(df, label, feature)
我通常会通过编写一个类似于此的函数来做到这一点:
my_chisq_function = function(df, label, feature) {
feature = enquo(feature)
label = enquo(label)
df %>%
chisq_test(!!label ~ !!feature)
}
但是当我运行它时:
my_chisq_function(df, cohort, gender)
我得到一个错误:
Error: The response variable `!` cannot be found in this dataframe.The response variable `!label` cannot be found in this dataframe.
关于如何让它工作的任何thoughts/suggestions?
谢谢,
D
我们可以在转换为字符串后构造一个公式
my_chisq_function <- function(df, label, feature) {
feature <- rlang::as_string(rlang::ensym(feature))
label <- rlang::as_string(rlang::ensym(label))
df %>%
infer::chisq_test(as.formula(stringr::str_c(label, feature, sep="~ ")))
}
my_chisq_function(df, cohort, gender)
或者另一种选择是使用 enexpr
和 rlang
中的 expr
my_chisq_function <- function(df, label, feature) {
df %>%
infer::chisq_test(rlang::expr(!! rlang::enexpr(label) ~
!! rlang::enexpr(feature)))
}
-测试
df1 <- mtcars
df1$carb <- as.factor(df1$carb)
df1$gear <- as.factor(df1$gear)
my_chisq_function(df1, carb, gear)
# A tibble: 1 x 3
# statistic chisq_df p_value
# <dbl> <int> <dbl>
#1 16.5 10 0.0857
substitute
的替代方案
my_chisq_function = function(df, label, feature) {
expr = substitute(chisq_test(x = df, label ~ feature))
eval(expr)
}
# test:
mtcars2 <- mtcars %>%
dplyr::mutate(cyl = factor(cyl), am = factor(am))
my_chisq_function(mtcars2, cyl, am)
## A tibble: 1 x 3
# statistic chisq_df p_value
# <dbl> <int> <dbl>
#1 8.74 2 0.0126
我正在使用推断包进行 运行 卡方检验,例如,
df %>%
chisq_test(label ~ feature)
我想把它放到一个函数中,这样我就可以写:
my_chisq_function(df, label, feature)
我通常会通过编写一个类似于此的函数来做到这一点:
my_chisq_function = function(df, label, feature) {
feature = enquo(feature)
label = enquo(label)
df %>%
chisq_test(!!label ~ !!feature)
}
但是当我运行它时:
my_chisq_function(df, cohort, gender)
我得到一个错误:
Error: The response variable `!` cannot be found in this dataframe.The response variable `!label` cannot be found in this dataframe.
关于如何让它工作的任何thoughts/suggestions?
谢谢, D
我们可以在转换为字符串后构造一个公式
my_chisq_function <- function(df, label, feature) {
feature <- rlang::as_string(rlang::ensym(feature))
label <- rlang::as_string(rlang::ensym(label))
df %>%
infer::chisq_test(as.formula(stringr::str_c(label, feature, sep="~ ")))
}
my_chisq_function(df, cohort, gender)
或者另一种选择是使用 enexpr
和 rlang
expr
my_chisq_function <- function(df, label, feature) {
df %>%
infer::chisq_test(rlang::expr(!! rlang::enexpr(label) ~
!! rlang::enexpr(feature)))
}
-测试
df1 <- mtcars
df1$carb <- as.factor(df1$carb)
df1$gear <- as.factor(df1$gear)
my_chisq_function(df1, carb, gear)
# A tibble: 1 x 3
# statistic chisq_df p_value
# <dbl> <int> <dbl>
#1 16.5 10 0.0857
substitute
my_chisq_function = function(df, label, feature) {
expr = substitute(chisq_test(x = df, label ~ feature))
eval(expr)
}
# test:
mtcars2 <- mtcars %>%
dplyr::mutate(cyl = factor(cyl), am = factor(am))
my_chisq_function(mtcars2, cyl, am)
## A tibble: 1 x 3
# statistic chisq_df p_value
# <dbl> <int> <dbl>
#1 8.74 2 0.0126