I have a large dataset and there are many different columns that I am trying to group the data by. I am trying to create a new column using dplyr and mutate which is the mean for each individual group. I then want to see the difference between these means and the mean of just one single category.

This question can pertain to the mtcars dataset. How would I group the mtcars data by "cyl" & "gear" and then take the mean of "mpg" for each group. I then want to see the difference of every group's mean of "mpg" compared to specifically all the cars with "gear"==5, but have variable "cyl".

I apologize if I'm asking the same question as others have, but I have not been able to find this specific question.

```
df <- mtcars
df2 <- df %>% group_by(cyl, gear) %>% mutate(mean_mpg = mean(mpg))
```

`df2 <- df %>% group_by(cyl, gear) %>% summarise(mean_mpg = mean(mpg))`

should get you startedgroup the mtcars data by "cyl" & "gear" and then take the mean of "mpg" for each group", meaning you have means for, e.g., cars with 4 cyl and 3 gears, 4 cyl and 4 gears, 4 cyl and 5 gears, etc. (Jack Brookes's answer covers this case). But your comment above seems to say you want means for 4, 6, 8 and cyl (ignoring gear), and compare those to means of 4, 6, and 8 cyl where gear is 5. I answered using my interpretation of you comment.Please edit your question to make your goal clear.