从dplyr中的先前值中减去组中的最后一个值

时间:2017-12-14 16:20:28

标签: r dplyr

我有以下数据

data = tribble(~t,~key,~value,
        1,"a",10,
        2,"a",20,
        3,"a",30,
        1,"b",100,
        2,"b",200,
        3,"b",300,
        1,"c",1000,
        2,"c",2000,
        3,"c",3000)

并希望得到以下结果

result = tribble(~t,~key,~value,
        1,"a",-20,
        2,"a",-10,
        3,"a",0,
        1,"b",-200,
        2,"b",-100,
        3,"b",0,
        1,"c",-2000,
        2,"c",-3000,
        3,"c",0)

我的想法是,我想从该组中的所有其他值中减去第3个值。我尝试group_by key,但在群内的行减法方面苦苦挣扎

1 个答案:

答案 0 :(得分:6)

我们可以使用last中的dplyr功能。 arrange函数用于确保数据集的顺序正确。

library(dplyr)
data2 <- data %>%
  arrange(key, t) %>%
  group_by(key) %>%
  mutate(value = value - last(value)) %>%
  ungroup()
data2
# # A tibble: 9 x 3
#       t   key value
#   <dbl> <chr> <dbl>
# 1     1     a   -20
# 2     2     a   -10
# 3     3     a     0
# 4     1     b  -200
# 5     2     b  -100
# 6     3     b     0
# 7     1     c -2000
# 8     2     c -1000
# 9     3     c     0