在“by"中使用因子列并且不要丢弃空因子 [英] Use a factor column in "by" and do not drop empty factors

查看:23
本文介绍了在“by"中使用因子列并且不要丢弃空因子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个 data.table:

Suppose I have a data.table:

x <- data.table(x=runif(3), group=factor(c('a','b','a'), levels=c('a','b','c')))

我想知道 x 中每个 group 有多少行:

I want to know how many rows in x exist for each group:

x[, .N, by="group"]
#    group N
# 1:     a 2
# 2:     b 1

问题:有没有办法强制上述by="group"考虑因素group的所有层次?

Question: is there some way to force the above by="group" to consider all levels of the factor group?

请注意,由于我在表中没有任何带有 group 'c' 的行,因此我没有得到 c 的行.

Notice how since I don't have any rows of with group 'c' in the table, I don't get a row for c.

期望的输出:

x[, .N, by="group", ???] # somehow use all levels in `group`
#    group N
# 1:     a 2
# 2:     b 1
# 3:     c 0

推荐答案

如果您愿意通过在 i 中枚举因子级别(而不是通过设置 by="group"),这将为您带来预期的结果.

If you are willing to run through the factor levels by enumerating them in i (rather than by setting by="group"), this will get you the hoped for results.

setkey(x, "group")
x[levels(group), .N, by=.EACHI]
#    group N
# 1:     a 2
# 2:     b 1
# 3:     c 0

这篇关于在“by"中使用因子列并且不要丢弃空因子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆