Take for example the mtcars data set. I would like to compare the ratio of mpg of cars that are grouped by cyl only , to the cars that are grouped by both cyl and carb. Problem is that grouping the dataset using dplyr creates one level of granularity which makes it impossible to compare to a different level of grouping. So what I did was create 2 new data sets, with each grouping, and then joined them together to compare the 2 means with a mutated column, as below. This worked, but it just seems like a a roundabout inefficient way to code . What is the proper way to do this? my code:
cyl_only <- mtcars %>%
group_by(cyl) %>%
summarise(cyl_only_mean= mean(mpg))
cyl_carb <- mtcars %>%
group_by(cyl,carb) %>%
summarise(cyl_carb_mean= mean(mpg))
cyl_carb_join <- cyl_only %>%
left_join(cyl_carb,by="cyl")
mtcars_result <- mutate(cyl_carb_join,ratio= cyl_only_mean/cyl_carb_mean)