Well, its complicated, in a way.
It has to do with what X is inside a grouping.
Consider these variations:
| description |
expression |
|---|
| Yours |
test[, X[Y == "a"], keyby=.(X) ] |
| X only |
test[, X, keyby=.(X) ] |
| Y=="a" only |
test[, Y == "a", keyby=.(X) ] |
X only gives:
> test[, X, keyby=.(X) ]
X X
1: 1 1
This is what 'X' is inside your grouping. Only that one value.
The third expression:
> test[, Y == "a", keyby=.(X) ]
X V1
1: 1 TRUE
2: 1 TRUE
3: 1 TRUE
There you see what Y == "a" looks like inside your grouping.
If you combine these, to do: X[ Y == "a" ] inside your grouping, you effectively do:
X <- 1
X[ c(TRUE,TRUE,TRUE) ]
X having only one value, but are asked to return the first, second and third values, will give you its one value and 2 NA's, which is what you see.
test[Y == 'a', .SD, keyby = .(X)]ortest[, .SD[Y == "a"], keyby = .(X)]- akruniis positive and exceedslength(x)then the corresponding selection isNA", together with the fact that inside each group, the grouping variable is of length 1 (see FAQ 2.10) - Henriki) is 3. The length of the grouping variable inside each group is 1 (x, the vector you try to index; again, see FAQ): "Ifiis positive (yes, hereiis 3) and exceedslength(x)(yes it does, length index vectoriis 3, length of vector to be indexedxis 1) then the corresponding selection isNA- Henrik