Language: R
Package: data.table
When I use data.table "by" to group several columns to calculate let's say the number of observations in each group, if a combination has zero entries, it does not show up in the output. However, even if it is empty I still want to see (as zero). Is there a way to do this?
# Example:
DT = data.table(A = c(1,1,1,1,2,2,2), B = c(F,F,T,F,T,T,T))
A B
1: 1 FALSE
2: 1 FALSE
3: 1 TRUE
4: 1 FALSE
5: 2 TRUE
6: 2 TRUE
7: 2 TRUE
DT[, j = .(.N), by = .(A,B)]
A B N
1: 1 FALSE 3
2: 1 TRUE 1
3: 2 TRUE 3
As you can see above the factor 2 in A has no corresponding F observation in column B. Thus, when grouped using data.table this entry will not be shown.
Edit:
It turns out a similar question was asked and answered before.
setkey(DT, A, B)
DT[CJ(A,B, unique = TRUE), j= .(.N), by= .EACHI]
A B N
1: 1 FALSE 3
2: 1 TRUE 1
3: 2 FALSE 0
4: 2 TRUE 3