Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

group by multiple variables without intersection

I want to group_by multiple columns wihout intersection.

I am looking for the output below without having to replicate the code for both variables.

library(dplyr)    
> mtcars %>% 
    +   group_by(cyl) %>%
    +   summarise(mean(disp))
    # A tibble: 3 × 2
        cyl `mean(disp)`
      <dbl>        <dbl>
    1     4         105.
    2     6         183.
    3     8         353.
    > 
    > mtcars %>% 
    +   group_by(am) %>%
    +   summarise(mean(disp))
    # A tibble: 2 × 2
         am `mean(disp)`
      <dbl>        <dbl>
    1     0         290.
    2     1         144.

I am not looking for the code below since this gives the intersection between the variables:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

> mtcars %>% 
+   group_by(cyl, am) %>%
+   summarise(mean(disp))
# A tibble: 6 × 3
# Groups:   cyl [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 

Thanks a lot!

>Solution :

An alternative would be a custom function:

my_func <- function(df, group){
  df %>% 
    group_by({{group}}) %>% 
    summarise(mean_disp = mean(disp))
}

my_func(mtcars, cyl)
my_func(mtcars, am)
    cyl mean_disp
  <dbl>     <dbl>
1     4      105.
2     6      183.
3     8      353.
> my_func(mtcars, am)
# A tibble: 2 × 2
     am mean_disp
  <dbl>     <dbl>
1     0      290.
2     1      144.
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading