Reputation: 93
I've looked around StackOverflow for an answer here, but I think I may be missing a term. Here's the scenario:
I have a large data set with multiple groups that I want to report on. Let's say that this data set has answers to certain questions as columns, and I want to take specific columns and responses, group the answers, and perform counts. Essentially, I have a dplyr filter expression that would look like this:
z <- results %>% filter(AgeGroup %in% c("16-20", "21-25", "26-30")) %>%
group_by(AgeGroup) %>% summarize(ageCount=n())
Then I generate a table with the results using xtable() and dump them in my Rmarkdown document. What I'd like to do is create a function that can do this, such that I can do the following
resultPrint <- function(qualifier, groupColumn) {
return(results %>% filter(qualifier) %>%
group_by(groupColumn) %>% summarize(count=n())
}
resultPrint("AgeGroup %in% c(\"16-20\", \"21-25\", \"26-30\")", "AgeGroup")
Or some equivalent.
Is there a way to do this in R? It would simplify a lot of code I am writing if I could. Thanks!
Upvotes: 1
Views: 92
Reputation: 93
Thank you to r2evans! Here's my solution:
resultPrint <- function(qualifier, groupColumn) {
return(results %>% filter_(qualifier) %>%
group_by_(.dots = groupColumn) %>% summarize(count=n()))
}
filterClause = quote(AgeGroup %in% c("16-20", "21-25", "26-30"))
stuff <- resultPrint(filterClause, quote(AgeGroup))
Thank you!!
Upvotes: 1