2

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())
Ian Campbell
  • 23,484
  • 14
  • 36
  • 57
danic10
  • 23
  • 4

2 Answers2

0

Get the data in long format, count, split on each name and use count_to_pct

library(dplyr)
library(tidyr)
library(purrr)

mtcars %>%
  pivot_longer(cols = c(vs, am, gear, carb)) %>%
  count(name, value) %>%
  group_split(name) %>%
  map(count_to_pct)

This is actually much simpler if you don't use count_to_pct function.

mtcars %>%
  pivot_longer(cols = c(vs, am, gear, carb)) %>%
  count(name, value) %>%
  group_by(name) %>%
  mutate(n = n/sum(n))


#  name  value      n
#   <chr> <dbl>  <dbl>
# 1 am        0 0.594 
# 2 am        1 0.406 
# 3 carb      1 0.219 
# 4 carb      2 0.312 
# 5 carb      3 0.0938
# 6 carb      4 0.312 
# 7 carb      6 0.0312
# 8 carb      8 0.0312
# 9 gear      3 0.469 
#10 gear      4 0.375 
#11 gear      5 0.156 
#12 vs        0 0.562 
#13 vs        1 0.438 
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
0

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()
})
starja
  • 9,887
  • 1
  • 13
  • 28