1

I'm trying to write a function to make it easier to create various summaries of data I have in a data frame. So far I have been manually writing all of these out in the following format:

issue_summary <- df %>%
  group_by(issue) %>%
  summarise(mean=mean(stdng),n())

Where "issue" is the name of a column in data frame "df". In order to permit me to generate these on the fly instead of writing one out for every single issue in the data set, I tried writing the following function, which does not work:

iss_summary <- function(df, x) {
  df %>%
    group_by(x) %>%
    summarise(mean=mean(stdng),n())
}

When I run this using iss_summary(df, issue), I get: "Error: Column x is unknown". I tried changing line 3 to group_by(paste0(x)), but running it with that I get: "Error in paste0(x): object 'issue' not found"

If I run the paste0 version as iss_summary(df, df$issue), it works, but having to type it out that way is annoying and I would like to understand why that works but neither the original version nor my attempted fix works. Thanks in advance!

zx8754
  • 52,746
  • 12
  • 114
  • 209
sinfony
  • 63
  • 5
  • use `!!as_name(issue)` inside of the group_by – SKyJim Mar 30 '20 at 22:18
  • 1
    @SKyJim that wouldn't work because the OP is passing unquoted. Here, it may be either `{{}}` or `!!enquo(issue)` – akrun Mar 30 '20 at 22:21
  • How do you intend to call the function? With bare column names like most dplyr functions, or names as strings? Depending on that, you should have some luck with [this](https://stackoverflow.com/q/27975124/5325862) or [this](https://stackoverflow.com/q/47524630/5325862) post – camille Mar 30 '20 at 23:06
  • Thanks folks! Not sure why this was reopened, it was marked closed because of the answers here, which did resolve my issue: https://stackoverflow.com/questions/27975124/pass-arguments-to-dplyr-functions – sinfony Mar 31 '20 at 21:07

0 Answers0