如何在使用data.table中的键进行子集时跳过组? [英] How can I skip groups while subsetting with key by in data.table?
本文介绍了如何在使用data.table中的键进行子集时跳过组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这个DT:
dt = data.table(ID = c(rep(letters [1:2] ,每个= 4),'b'),值= seq(1,9))
ID值
1:a 1
2:a 2
3:a 3
4:a 4
5:b 5
6:b 6
7:b 7
8:b 8
9:b 9
我需要在子集化时消除组,但只有当数据满足某些条件时才需要。像这样的东西是行不通的:
dt [,{if(.N == 4).SD else NULL $ b $为了我需要删除那些不需要的组,我们需要删除那些不需要的组。符合条件。在这个例子中,我想跳过长度不同于4的组。为了得到:
ID值
1:a 1
2:a 2
3:a 3
4:a 4
但是我一直无法工作,希望有任何帮助。
解决方案
p> @jangorecki想出了答案:
does dt [,if(.N == 4).SD,by =ID ]
回答你的问题?
I have this DT:
dt=data.table(ID=c(rep(letters[1:2],each=4),'b'),value=seq(1,9))
ID value
1: a 1
2: a 2
3: a 3
4: a 4
5: b 5
6: b 6
7: b 7
8: b 8
9: b 9
I need to eliminate groups while subsetting but only when the data fulfils some condition. Something like this does not work:
dt[,{if (.N==4) .SD else NULL
v1},by="ID"]
So that I need to remove the groups that do not meet the condition. In this example I would like to skip the groups which length is different than 4. So that I get:
ID value
1: a 1
2: a 2
3: a 3
4: a 4
But I haven't been able to work this around, I would appreciate any help.
解决方案
@jangorecki came up with the answer:
does dt[, if (.N==4) .SD, by="ID"]
answer your question?
这篇关于如何在使用data.table中的键进行子集时跳过组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文