使用dplyr :: case_when进行简洁的评估编程 [英] Tidy evaluation programming with dplyr::case_when
问题描述
我尝试编写一个简单的函数,环绕dplyr :: case_when()函数.我在 https://cran.r-project.org/web/packages/dplyr/vignettes/programming.html ,但无法弄清case_when()函数如何工作.
I try to write a simple function wrapping around the dplyr::case_when() function. I read the programming with dplyr documentation on https://cran.r-project.org/web/packages/dplyr/vignettes/programming.html but can't figure out how this works with the case_when() function.
我有以下数据:
data <- tibble(
item_name = c("apple", "bmw", "bmw")
)
以及以下列表:
cat <- list(
item_name == "apple" ~ "fruit",
item_name == "bmw" ~ "car"
)
然后我想编写一个像这样的函数:
Then I would like to write a function like:
category_fn <- function(df, ...){
cat1 <- quos(...)
df %>%
mutate(category = case_when((!!!cat1)))
}
不幸的是,在这种情况下,category_fn(data,cat)
给出评估错误.我想获得与通过以下方式获得的输出相同的输出:
Unfortunately category_fn(data,cat)
gives an evaluation error in this case. I would like to obtain the same output as the output obtained by:
data %>%
mutate(category = case_when(item_name == "apple" ~ "fruit",
item_name == "bmw" ~ "car"))
这是怎么做的?
推荐答案
首先引用列表中的每个元素:
Quote each element of your list first:
cat <- list(
quo(item_name == "apple" ~ "fruit"),
quo(item_name == "bmw" ~ "car")
)
然后,您的函数不必引用cat对象本身.我还更改了其他所有..."参数的用法,以在调用中明确引用类别参数:
Your function does not then have to quote the cat object itself. I have also changed the use of the "everything else" ... argument to refer to the category argument explicitly in the call:
category_fn <- function(df, categories){
df %>%
mutate(category = case_when(!!!categories))
}
该函数的输出如预期的那样:
The output of the function is then as expected:
category_fn(data, cat)
# A tibble: 3 x 2
item_name category
<chr> <chr>
1 apple fruit
2 bmw car
3 bmw car
为完整起见,我注意到类别列表也可以在使用基本R quote()函数定义时与您的函数一起使用:
For completeness, I note that the category list works with your function when defined using the base R quote() function too:
cat <- list(
quote(item_name == "apple" ~ "fruit"),
quote(item_name == "bmw" ~ "car")
)
> cat
[[1]]
item_name == "apple" ~ "fruit"
[[2]]
item_name == "bmw" ~ "car"
> category_fn(data, cat)
# A tibble: 3 x 2
item_name category
<chr> <chr>
1 apple fruit
2 bmw car
3 bmw car
这篇关于使用dplyr :: case_when进行简洁的评估编程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!