Hello experts. I think this is a somewhat advanced topic. Let me start with an example code.
test_data = tibble(a = sample(c("group 1","group 2"), 100, replace=T),
b = rnorm(100))
test_fun = function(data, group, value) {
data %>% group_by(group) %>% summarize(mean=mean(value))
}
test_fun(test_data, a, b)
#Error: Must group by variables found in `.data`.
#* Column `group` is not found.
test_fun(test_data, quote(a), quote(b))
#Error: Must group by variables found in `.data`.
#* Column `group` is not found.
As shown in the example, I want to make a function that takes a tibble and unquoted variable names as arguments and do some data transformation with tidyverse workflow. I expected quote(variable) would work, but it didn't (I don't quite understand why). How can I make this function work?
library(dplyr)
#>
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union
test_data = tibble(a = sample(c("group 1","group 2"), 100, replace=T),
b = rnorm(100))
test_fun = function(data, group, value) {
data %>% group_by({{ group}} ) %>% summarize(mean=mean({{ value }}))
}
test_fun(test_data, a, b)
#> `summarise()` ungrouping output (override with `.groups` argument)
#> # A tibble: 2 x 2
#> a mean
#> <chr> <dbl>
#> 1 group 1 0.0526
#> 2 group 2 0.241