In the StackOverflow question about rolling sum it was given an answer involving the use of a list of values into i
and by=
data.table arguments.
This feature did not seems obvious to me when a looked into the package manual or vignettes.
In the quest to understand this feature I created some simple code with some unintuitive results where I am just trying to pass different lists of index and groups to the functions i
and by
and see what output was given by the function j
.
library(data.table)
DT1 <- data.table(x = c(1, 2, 3), y = c('a', 'b', 'b'))
DT2 <- data.table(x = c(1, 2, 3, 4) , y = c('a', 'a', 'b', 'c'))
DT1[, idx := .I]
DT2[, idx := .I]
DT1[DT2$x, idx, by = DT2$y]
DT2 idx
1: a 1
2: a 2
3: b 3
4: c 0
DT1[DT2$x, x, by = DT2$y]
DT2 x
1: a 1.000000e+00
2: a 2.000000e+00
3: b 3.000000e+00
4: c 1.919019e-316
DT1[DT2$idx, x, by = DT2$y]
DT2 x
1: a 1.000000e+00
2: a 2.000000e+00
3: b 3.000000e+00
4: c 1.919019e-316
Can someone explain this feature and why this simple example give a bogus result?
I was expecting to this code return errors since I am passing groups and indexes of DT2 that are not in DT1.