如果 != 从表中排除
如果我使用 table()
我可以通过执行以下操作来排除元素:
b <- factor(rep(c("A","B","C"), 10))
table(b, exclude="B")
现在,如果我想排除除“B”之外的所有内容该怎么办?
我已经尝试过了 table(b, exlcude=!"B")
但它没有起作用。 table()
函数是否允许此功能?
If I'm using the table()
I can exclude elements by doing:
b <- factor(rep(c("A","B","C"), 10))
table(b, exclude="B")
Now what if I want to exclude everything but "B"?
I have triedtable(b, exlcude=!"B")
but it hasn't worked. does the table()
function allow this functionality?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要首先收集具有级别“B”的特定因子变量中的所有可能级别。可能有更简单的方法可以做到这一点,但假设发生所需排除的因素是“fac1”,那么也许是这样的:
我可能会用
subset
来减少“双重否定”:You would need to first gather all the possible levels in the particular factor variable(s) that has(have) a level "B". There are probably easier ways to do this, but assuming the factor in which the desired exclusion occurs is 'fac1' then perhaps something like:
I would probably have done it less "double negatively" with
subset
:您可以使用 setdiff:
当然,您也可以只对表对象进行子集化:
You can use
setdiff
:Of course, you may as well just subset your table object:
就像简单一样,
但是为了计算一个值的出现次数,不需要使用 table。更简单的是这样的:
As simple as
But for counting occurences of one value, there's no need to use table. Much simpler is this: