How to merge two dataframes using multiple columns as key?

前端 未结 2 395
感情败类
感情败类 2021-01-13 05:33

Say I have the following dataframes:

DF1 <- data.frame(\"A\" = rep(c(\"A\",\"B\"), 18),
                  \"B\" = rep(c(\"C\",\"D\",\"E\"), 12),
                  


        
2条回答
  •  悲哀的现实
    2021-01-13 06:27

    As Akrun mentioned in comments, your lookup table (DF2) needs to be reduced to just its unique A/B combinations. For your current dataframe, this isn't a problem, but you will need additional rules if there are multiple possible values for the same combination. From there, the solution is easy:

    DF2.u <- unique(DF2)
    DF3 <- merge(DF1, DF2.u, all = T)
    

    Note that this will produce a new dataframe with an empty TEST column (all values NA), and a VAL column assigned from DF2. To do exactly what you wanted (replace TEST with VAL where possible), here is some slightly clunkier code:

    DF1$TEST <- merge(DF1, DF2.u, all = T)$VAL
    

    EDIT: in response to your question, you can boil down DF2 if necessary quite simple:

    DF2$C <- c(1:12) #now unique() won't work
    DF2.u <- unique(DF2[1:3])
    
     A B VAL
    1 A C   1
    2 A D   3
    

提交回复
热议问题