我试图用这个典型例子来说明卷曲运算符的工作原理
mean_by <- function(data, by, var) {
data %>%
dplyr::group_by({{ by }}) %>%
dplyr::summarise(avg = mean({{ var }}, na.rm = TRUE))
}
mtcars %>% mean_by(by = cyl, var = disp)
并通过向分组添加一个额外的变量来修改它。唉,我可能永远都不明白这个操作符到底做了什么;我尝试做的事情如下
mean_by <- function(data, by, var) {
data %>%
dplyr::group_by({{ c(by, hp) }}) %>%
dplyr::summarise(avg = mean({{ var }}, na.rm = TRUE))
}
mtcars %>% mean_by(by = cyl, var = disp)
显然不起作用