I am trying to automate this code and can't figure out how to use the apply or map functions to do it!
This is the set-up:
data("mtcars")
count_to_pct <- function(data,..., col = n) {
grouping_vars_expr<- quos(...)
col_expr<- enquo(col)
data %>%
group_by(!!! grouping_vars_expr) %>%
mutate(pct = (!! col_expr) / sum(!! col_expr)) %>%
ungroup()
}
Here is where the problem comes in: repetitive code! Trying to clean it up for my own sanity. How can I pass a list through data %>% count(column) %>% count_to_pct()
?
dataframes<- list(
mtcars %>% count(vs) %>% count_to_pct(),
mtcars %>% count(am) %>% count_to_pct(),
mtcars %>% count(gear) %>% count_to_pct(),
mtcars %>% count(carb) %>% count_to_pct())