Search code examples
rdplyrrlang

How to apply multiple columns to make a summary list of dataframes in R


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())

Solution

  • If you reference your column names by the character name, you can use lapply and rlang::sym to convert the character name to the column symbol that can be used inside dplyr, see here:

    dataframes_list <- lapply(c("vs", "am", "gear", "carb"), function(x) {
      mtcars %>% count(!!rlang::sym(x)) %>% count_to_pct()
    })