Refer: http://dplyr.tidyverse.org/articles/programming.html
This code works fine:
df <- tibble(
g1 = c(1, 1, 2, 2, 2),
g2 = c(1, 2, 1, 2, 1),
a = sample(5),
b = sample(5)
)
my_summarise <- function(df, group_by) {
group_by <- enquo(group_by)
print(group_by)
df %>%
group_by(!!group_by) %>%
summarise(a = mean(a))
}
my_summarise(df, g1)
However, if we wrap this function in another and make a call, it will not work. Is it because the name is passed only for one level?
wrapped_my_Summarize <- function(wdf, w_group_by){
my_summarise(wdf, w_group_by)
}
wrapped_my_Summarize(df, g1)
All in all, I feel like the above example is risky to go with
source
share