Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calculate difference from the group mean using dplyr

Tags:

r

dplyr

I want to calculate the difference each row has from its group's mean. Is there a way to do this without creating an intermediate table and joining it?

group_summary <- mtcars %>%
  group_by(cyl) %>%
  summarize(mean_mpg = mean(mpg))

left_join(mtcars, group_summary) %>%
  mutate(mpg_diff_from_group = mpg - mean_mpg)
like image 933
Jeremy Avatar asked Jan 24 '26 04:01

Jeremy


1 Answers

Yes, the following works without intermediate table:

mtcars %>%
    group_by(cyl) %>%
    mutate(grouped_diff = mpg - mean(mpg)) %>%
    ungroup()
like image 182
Konrad Rudolph Avatar answered Jan 25 '26 22:01

Konrad Rudolph



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!