3
votes

I have the dt and dt1 data.tables.

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

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)],]

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...

Thank you in advance for your help!

2

2 Answers

8
votes
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
4
votes

Use merge:

merge(dt,dt1, by=c("year","id"))
   year id event performance
1: 2005  2     1        1000
2: 2006  2     0        1001
3: 2007  2     0        1002