data.table:如果不是所有的变量都在组内,如何用%in%过滤行? [英] data.table: how to filter lines with %in% when not all variables are inside the group?

查看:113
本文介绍了data.table:如果不是所有的变量都在组内,如何用%in%过滤行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能是一个简单的问题,我错过了一些东西。但它是在欺骗我。假设这个例子是data.table:

This may be a simple question and I am missing something. But It is bugging me. Assume this example data.table:

library(data.table)
test <- data.table(group1 = "a", group2 = "z", value  = 1)

test[group1 %in% c("a", "b"), sum(value), group2]
Erro em `[.data.table`(test, group1 %in% c("a", "b"), sum(value), group2) : 
  i[2] is 0. While grouping, i=0 is allowed when it's the only value. When length(i) > 1, all i should be > 0.

但这样做:

test[group1 %in% c("a", "b"), ][,sum(value), group2]
   group2 V1
1:      z  1

这真的是期望的行为吗?

Is this really the expected behavior?

推荐答案

更新:此行为已修复开发版本,1.9.5 并按照预期工作。

Update: This behaviour is fixed in the development version, 1.9.5 and works as expected now.

另一种解决方法是使用%chin%中内置的 data.table 函数

Another workaround is to use data.table built in %chin% function

test[group1 %chin% c("a", "b"), sum(value), group2]
#    group2 V1
# 1:      z  1

这篇关于data.table:如果不是所有的变量都在组内,如何用%in%过滤行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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