I calculate number of rows by group, where the grouping variable is a factor. I want also factor levels which are not represented in the data, i.e. have zero rows, to be included in the result.
A small data example with variable 'x', a factor with levels c("a", "b", "c")
library(data.table)
DT = data.table(x = factor(rep(c("b", "a", "c"), each = 3)))
The data is filtered, e.g. all rows of x == "c"
are removed, and number of rows by group is calculated. However, the zero count of level "c" is not shown in the result:
DT[x != "c"][, .N, by = x]
x N
<fctr> <int>
1: b 3
2: a 3
The desired result should include also the zero count of "c":
x N
<fctr> <int>
1: b 3
2: a 3
3: c 0 # <--
Is there some way to get this output?