0

I wish to calculate the difference between the current row and previous row, by groups.

x = data.table(a=c(15, 25, 10, 12), b = c(1,1,2,2))
> x
    a b
1: 15 1
2: 25 1
3: 10 2
4: 12 2
> x[, c:= a - c(NA, a[.I-1]), by=b]
Warning messages:
1: In a - c(NA, a[.I - 1]) :
  longer object length is not a multiple of shorter object length
2: In `[.data.table`(x, , `:=`(c, a - c(NA, a[.I - 1])), by = b) :
  RHS 1 is length 3 (greater than the size (2) of group 2). The last 1 element(s) will be discarded.

What I wish to obtain is:

> x
    a b  c
1: 15 1 NA
2: 25 1 10
3: 10 2 NA
4: 12 2  2
Timothée HENRY
  • 14,294
  • 21
  • 96
  • 136

1 Answers1

3
#Using development version of data.table    

x[,value:=a-shift(a,1,type="lag"),by=b]
    a b value
1: 15 1    NA
2: 25 1    10
3: 10 2    NA
4: 12 2     2
user227710
  • 3,164
  • 18
  • 35