merging tables with different column names

后端 未结 1 1057
说谎
说谎 2020-12-30 10:48

If I understand correctly, by default data.table merges two tables by comparing columns which are set as keys and have same names. How should I write if I have

相关标签:
1条回答
  • 2020-12-30 11:21

    Using data.table's subset based joins along with the recently implemented on= argument and nomatch=0L, this is simply:

    DT2[DT1, on=c(col5="col2", col4="col3"), nomatch=0L]
    

    See the secondary indices vignette for more.


    Alternatively if you've the data.tables keyed, then you can skip the on= argument. But the solution above would be idiomatic as it retains the order of original data.tables, and it is clear to tell what columns are being looked up by looking at the code.

    setkey(DT1, col2, col3)
    setkey(DT2, col5, col4)
    DT2[DT1, nomatch=0L]
    

    See history for older versions.

    0 讨论(0)
提交回复
热议问题