Conditional binary join and update by reference using the data.table package

前端 未结 2 1251
轮回少年
轮回少年 2020-11-29 11:28

So here is my real life problem which I feel like can be easily solved and I\'m missing something obvious here. I have two big data sets called TK and DFT

相关标签:
2条回答
  • 2020-11-29 12:06

    Copying from Arun's updated answer here

    TK[venue_id %in% 1:2, New_id := DFT[.SD, New_id]][]
    #    venue_id DFT_id New_id
    # 1:        1      1      3
    # 2:        2      1      3
    # 3:        1      2      4
    # 4:        3      2   9401
    # 5:        2      3      2
    # 6:        3      3    456
    

    His answer gives the details of what is going on.

    0 讨论(0)
  • 2020-11-29 12:08

    Here's a very simple approach:

    TK[DFT, New_id := ifelse(venue_id %in% 1:2, i.New_id, New_id)][]
    #    venue_id DFT_id New_id
    # 1:        1      1      3
    # 2:        2      1      3
    # 3:        1      2      4
    # 4:        3      2   9401
    # 5:        2      3      2
    # 6:        3      3    456
    

    I haven't checked, but I suspect the other answer is faster.

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