In the help page for special-symbols in data.table, it says ".N
can be used in i
as well." How do I do that?
For example, I would expect the following code to keep only rows where there is one element in the group.
> library(data.table)
> set.seed(734)
> dt <- data.table(x = c(rep("a", 5), rep("b", 3), "c", "d", "e"),
y = runif(11))
> dt
x y
1: a 0.46431448
2: a 0.57148294
3: a 0.30197960
4: a 0.06394102
5: a 0.08793526
6: b 0.62994539
7: b 0.64693916
8: b 0.79671939
9: c 0.60865117
10: d 0.86025196
11: e 0.21562992
> dt[.N == 1, .(y), by = .(x)]
Empty data.table (0 rows) of 2 cols: x,y
I would have expected this to have the same result as:
> dt[, .(n = .N, y = y), by = .(x)][n == 1, .(x, y)]
x y
1: c 0.6086512
2: d 0.8602520
3: e 0.2156299
If not like the example above, how would I use .N
in i
for data.table
?