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!