I want to use group_by in a function, the following is my code, 1, 2 work well, so I create a function - 3, while it doesn't work in 4. I don't known how to address this problem, so ask for a help.
# 1 generate variables and dataframe
x <- rnorm(100)
y <- rep(c("A", "B"), 50)
df <- data.frame(y, x)
# 2 group by y
df %>%
group_by(y) %>%
summarise(n = n(),
mean = mean(x),
sd = sd(x))
# 3 create function
group <- function(df, var1, var2){
df %>%
group_by(var1) %>%
summarise(n = n(),
mean = mean(var2),
sd = sd(var2))
}
# 4 test function
group(df = df, var1 = y, var2 = x)
# the error is as follows:
"Error in grouped_df_impl(data, unname(vars), drop) :
Column `var1` is unknown
Called from: grouped_df_impl(data, unname(vars), drop)",