2

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)",

1 Answer 1

4

You can do:

library(dplyr)
group <- function(df, var1, var2){
  var1 <-  enquo(var1); var2 <-  enquo(var2);
  df %>% 
    group_by(!!var1) %>% 
    summarise(n = n(),
              mean = mean(!!var2),
              sd = sd(!!var2))
}

group(df = df, var1 = y, var2 = x)
### A tibble: 2 x 4
##  y         n    mean    sd
##  <fct> <int>   <dbl> <dbl>
##1 A        50 -0.133  0.866
##2 B        50  0.0770 0.976

For further reference check the link

Sign up to request clarification or add additional context in comments.

1 Comment

You are great!! This question have annoyed me for a long time, thanks for your kindly help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.