FrancoBattiato
FrancoBattiato

Reputation: 53

Group_by per one variable and for each variable in a list and then summarise

I have list of variables. For instance this:

mtcars_list <- c ("vs","am","gear","carb")

I have also another variable I want to group_by. In this case: "cyl" And the variable I want to summarise. In this case: "disp"

I want to group_by for all the variables in this mtcars_list. In this example below is "vs" but I would like a map or similar function that groups_by per "cyl" and also per each one of the variables in the list. How can I do this?

mtcars %>% 
  group_by(cyl, vs) %>% 
  summarise(mean(disp))

The results would be: 6 cyl and 0 vs, a mean.

6 cyl and 0 vs, another mean.

4 cyl and 1 vs, another mean, etc.

and so on with cyl and each variable of mtcars_list

Thank you!

Upvotes: 0

Views: 32

Answers (1)

stefan
stefan

Reputation: 124213

Using purrr::map and the .data pronoun from rlang you could do:

mtcars_list <- c("vs", "am", "gear", "carb")

library(dplyr, warn=FALSE)
library(purrr)

purrr::map(mtcars_list, ~ mtcars %>%
  group_by(cyl, .data[[.x]]) %>%
  summarise(mean(disp), .groups = "drop"))
#> [[1]]
#> # A tibble: 5 × 3
#>     cyl    vs `mean(disp)`
#>   <dbl> <dbl>        <dbl>
#> 1     4     0         120.
#> 2     4     1         104.
#> 3     6     0         155 
#> 4     6     1         205.
#> 5     8     0         353.
#> 
#> [[2]]
#> # A tibble: 6 × 3
#>     cyl    am `mean(disp)`
#>   <dbl> <dbl>        <dbl>
#> 1     4     0        136. 
#> 2     4     1         93.6
#> 3     6     0        205. 
#> 4     6     1        155  
#> 5     8     0        358. 
#> 6     8     1        326  
#> 
#> [[3]]
#> # A tibble: 8 × 3
#>     cyl  gear `mean(disp)`
#>   <dbl> <dbl>        <dbl>
#> 1     4     3         120.
#> 2     4     4         103.
#> 3     4     5         108.
#> 4     6     3         242.
#> 5     6     4         164.
#> 6     6     5         145 
#> 7     8     3         358.
#> 8     8     5         326 
#> 
#> [[4]]
#> # A tibble: 9 × 3
#>     cyl  carb `mean(disp)`
#>   <dbl> <dbl>        <dbl>
#> 1     4     1         91.4
#> 2     4     2        117. 
#> 3     6     1        242. 
#> 4     6     4        164. 
#> 5     6     6        145  
#> 6     8     2        346. 
#> 7     8     3        276. 
#> 8     8     4        406. 
#> 9     8     8        301

Or using lapply:

lapply(mtcars_list, \(x) mtcars %>%
  group_by(cyl, .data[[x]]) %>%
  summarise(mean(disp), .groups = "drop"))

Upvotes: 1

Related Questions