Joining two data.tables in R based on multiple keys and duplicate entries

前端 未结 1 718
无人及你
无人及你 2020-12-18 10:45

I am trying to join two data.tables in R base don multiple setkeys and which have repeated entries. As an example

>DT1
ID  state Month Day Year
1   IL             


        
相关标签:
1条回答
  • 2020-12-18 11:13

    Just helped a friend with this (he couldn't find a good Stack Overflow answer) so I figured this question needed a more complete "toy" answer.

    Here's a couple of simple data tables with one mismatched key:

    dt1 <- data.table(a = LETTERS[1:5],b=letters[1:5],c=1:5)
    dt2 <- data.table(c = LETTERS[c(1:4,6)],b=letters[1:5],a=6:10)
    

    And here's several multiple key merge options:

    merge(dt1,dt2,by.x=c("a","b"),by.y=c("c","b")) #Inner Join
    merge(dt1,dt2,by.x=c("a","b"),by.y=c("c","b"),all=T) #Outer Join
    
    setkey(dt1,a,b)
    setkey(dt2,c,b)
    
    dt2[dt1] #Left Join (if dt1 is the "left" table)
    dt1[dt2] #Right Join (if dt1 is the "left" table)
    
    0 讨论(0)
提交回复
热议问题