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)
Yes, the following works without intermediate table:
mtcars %>%
group_by(cyl) %>%
mutate(grouped_diff = mpg - mean(mpg)) %>%
ungroup()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With