首页 > 解决方案 > Calculate difference from the group mean using 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)

标签: rdplyr

解决方案


Yes, the following works without intermediate table:

mtcars %>%
    group_by(cyl) %>%
    mutate(grouped_diff = mpg - mean(mpg)) %>%
    ungroup()

推荐阅读