Subsetting a data.table using another data.table

前端 未结 2 826
情书的邮戳
情书的邮戳 2021-01-06 11:43

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=         


        
相关标签:
2条回答
  • 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
    
    0 讨论(0)
  • 2021-01-06 12:13
    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
    
    0 讨论(0)
提交回复
热议问题