我正在尝试获取连续行之间的差异,例如:

dt_in <- data.table(
  Game = c(1,1,2,2,3,3,4,4),
  ID = c(1,2,3,4,5,6,7,8),
  weight = c(150,120,151,160,190,170,170,170)
)

我想为每个 ID 计算它们的权重比 Game 中的另一个 ID 多多少少。到目前为止我所做的是:
dt_in[, d1 := diff(weight), by = list(Game)]

这使:
> dt_in
   Game ID weight  d1
1:    1  1    150 -30
2:    1  2    120 -30
3:    2  3    151   9
4:    2  4    160   9
5:    3  5    190 -20
6:    3  6    170 -20
7:    4  7    170   0
8:    4  8    170   0

但我想要的是:
> dt_in
   Game ID weight  d1 want
1:    1  1    150 -30   30
2:    1  2    120 -30  -30
3:    2  3    151   9   -9
4:    2  4    160   9    9
5:    3  5    190 -20   20
6:    3  6    170 -20  -20
7:    4  7    170   0    0
8:    4  8    170   0    0

最佳答案

我们可以得到按 'Game' 分组的 'weight' 的 diff,乘以 -1 并连接两个值。

dt_in[, want := {v1 <- diff(weight); list(c(-v1, v1))} , by = Game]

dt_in
#    Game ID weight want
#1:    1  1    150   30
#2:    1  2    120  -30
#3:    2  3    151   -9
#4:    2  4    160    9
#5:    3  5    190   20
#6:    3  6    170  -20
#7:    4  7    170    0
#8:    4  8    170    0

或者@Frank 的紧凑选项
dt_in[, want := c(-1,1)*diff(weight), by=Game]

关于r - R中使用数据表的连续行组之间的差异,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37872409/

10-12 23:21