使用另一个data.table子集设置data.table [英] Subsetting a data.table using another data.table

查看:166
本文介绍了使用另一个data.table子集设置data.table的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 dt dt1 data.table s。

dt<-data.table(id=c(rep(2, 3), rep(4, 2)), year=c(2005:2007, 2005:2006), event=c(1,0,0,0,1))
dt1<-data.table(id=rep(2, 5), year=c(2005:2009), performance=(1000:1004))

dt

   id year event
1:  2 2005     1
2:  2 2006     0
3:  2 2007     0
4:  4 2005     0
5:  4 2006     1

dt1

   id year performance
1:  2 2005        1000
2:  2 2006        1001
3:  2 2007        1002
4:  2 2008        1003
5:  2 2009        1004

我想使用第一列和第二列的组合来子集前者,它也出现在 dt1 。因此,我想创建一个新对象,而不覆盖 dt 。这是我想要的。

I would like to subset the former using the combination of its first and second column that also appear in dt1. As a result of this, I would like to create a new object without overwriting dt. This is what I'd like to obtain.

   id year event
1:  2 2005     1
2:  2 2006     0
3:  2 2007     0



我尝试使用以下代码:

I tried to do this using the following code:

dt.sub<-dt[dt[,c(1:2)] %in% dt1[,c(1:2)],]

结果,我得到了一个与 dt 相同的数据表。我认为我的代码至少有两个错误。第一个是我可能使用错误的方法按列子集化 data.table 。第二个,很明显,%in%适用于向量而不是多列对象。无论如何,我找不到更有效的方法来做...

but it didn't work. As a result, I got back a data table identical to dt. I think there are at least two mistakes in my code. The first is that I am probably subsetting the data.table by column using a wrong method. The second, and pretty evident, is that %in% applies to vectors and not to multiple-column objects. Nevertherless, I am unable to find a more efficient way to do it...

预先感谢您的帮助!

推荐答案

setkeyv(dt,c('id','year'))
setkeyv(dt1,c('id','year'))
dt[dt1,nomatch=0]



-

Output -

> dt[dt1,nomatch=0]
   id year event performance
1:  2 2005     1        1000
2:  2 2006     0        1001
3:  2 2007     0        1002

这篇关于使用另一个data.table子集设置data.table的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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