r - Use a factor column in "by" and do not drop empty factors -
suppose have data.table:
x <- data.table(x=runif(3), group=factor(c('a','b','a'), levels=c('a','b','c')))
i want know how many rows in x
exist each group
:
x[, .n, by="group"] # group n # 1: 2 # 2: b 1
question: there way force above by="group"
consider levels of factor group
?
notice how since don't have rows of group
'c' in table, don't row c.
desired output:
x[, .n, by="group", ???] # somehow use levels in `group` # group n # 1: 2 # 2: b 1 # 3: c 0
if willing run through factor levels enumerating them in i
(rather setting by="group"
), hoped results.
setkey(x, "group") x[levels(group), .n, by=.eachi] # group n # 1: 2 # 2: b 1 # 3: c 0
Comments
Post a Comment