在dplyr中使用group_by时处理缺少的字符串值到函数 [英] Handling a missing string value to a function when using group_by in dplyr
问题描述
我希望创建一个可以接受多个字符串输入的函数(在此示例中为2个),并使用group_by返回即使返回一个字符串也返回结果。我知道我可以创建if语句来解决只将一个字符串传递给函数的情况,但是group_by是否有更好的方法可以在不构建条件语言的情况下仍然产生输出(即,使用多个输入变得更加麻烦)。
I'm looking to create a function that can take multiple string inputs (2 in this example), and using group_by, return results even if only one string is input. I know I could create if statements to get around the case when only one string is passed to the function, but is there a better way for group_by to still produce output without building in conditional language (i.e., gets more cumbersome with multiple inputs).
可复制的示例
library(dplyr)
# Create simple function
car_fx <- function(df, grp1, grp2) {
output <- df %>%
group_by(.data[[grp1]], .data[[grp2]]) %>%
summarize(mean_hp = mean(hp, na.rm = TRUE))
}
# String inputs
grp1 <- "cyl"
grp2 <- "carb"
# Run and print function output
(car_fx(mtcars, grp1, grp2))
# works fine
# A tibble: 9 x 3
# Groups: cyl [3]
cyl carb mean_hp
<dbl> <dbl> <dbl>
1 4 1 77.4
2 4 2 87
3 6 1 108.
4 6 4 116.
5 6 6 175
6 8 2 162.
7 8 3 180
8 8 4 234
9 8 8 335
如果仅传递一个变量,则该函数将引发错误。我想做的是让函数的行为就像我仅传递单个变量一样,并且可以在可能创建3个或更多变量输入的函数中使用。
If I only pass one variable, the function throws an error. What I'd like to do is have the function behave as if I only passed the single variable, and be able to use in function where I might create 3 or more variable inputs.
# Try with just one group, including with NA. Throws error.
(car_fx(mtcars, grp1))
(car_fx(mtcars, grp1, NA))
推荐答案
您可以使用省略号 ...
将任意数量的参数传递给函数。在这种情况下,要在group_by函数中使用的任何列名称。
You can use ellipsis ...
to pass an arbitrary number of arguments to a function. In this case any column names you want to use in the group_by function.
# Create simple function
car_fx <- function(df, ...) {
output <- df %>%
group_by_at(c(...)) %>%
summarize(mean_hp = mean(hp, na.rm = TRUE))
}
这篇关于在dplyr中使用group_by时处理缺少的字符串值到函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!