Operations between groups with dplyr

你离开我真会死。 提交于 2021-01-28 18:20:34

问题


I have a data frame as follow where I would like to group the data by grp and index and use group a as a reference to perform some simple calculations. I would like to subtract the variable value from other group from the values of group a.

df <- data.frame(grp = rep(letters[1:3], each = 2), 
                 index = rep(1:2, times = 3), 
                 value = seq(10, 60, length.out = 6))

df


##   grp index value
## 1   a     1    10
## 2   a     2    20
## 3   b     1    30
## 4   b     2    40
## 5   c     1    50
## 6   c     2    60

The desired outpout would be like:

##   grp index value
## 1   b     1    20
## 2   b     2    20
## 3   c     1    40
## 4   c     2    40

My guess is it will be something close to:

group_by(df, grp, index) %>% 
  mutate(diff = value - value[grp == "a"])

Ideally I would like to do it using dplyr.

Regards, Philippe


回答1:


We can filter for 'grp' that are not 'a' and then do the difference within mutate.

df %>%
   filter(grp!="a") %>%
   mutate(value = value- df$value[df$grp=="a"])

Or another option would be join

df %>% 
  filter(grp!="a")  %>%
  left_join(.,  subset(df, grp=="a", select=-1), by = "index") %>% 
  mutate(value = value.x- value.y)  %>% 
  select(1, 2, 5)
#   grp index value
#1   b     1    20
#2   b     2    20
#3   c     1    40
#4   c     2    40


来源:https://stackoverflow.com/questions/36736289/operations-between-groups-with-dplyr

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!