match two data.frames based on multiple columns

后端 未结 3 1557
被撕碎了的回忆
被撕碎了的回忆 2021-01-11 17:19

My head stands still at the moment. I would like to match/extract data from a larger data.frame (df) based on the columns in a smaller data.frame (mdf). What I\'m getting st

相关标签:
3条回答
  • 2021-01-11 17:42

    Another way would be:

    library(dplyr)
    inner_join(df, mdf)
    
    #Joining by: c("car_1", "car_2")
    #              car_1          car_2  mpg cyl disp
    #1        Datsun 710 Ford Pantera L 22.8   4  108
    #2    Hornet 4 Drive   Ferrari Dino 21.4   6  258
    #3 Hornet Sportabout  Maserati Bora 18.7   8  360
    #4           Valiant     Volvo 142E 18.1   6  225
    
    0 讨论(0)
  • 2021-01-11 17:53

    How about merge(df, mdf, all.x = FALSE, all.y = TRUE)?

    Edit: If you have different column names you can specify which ones to merge on, e.g.:

    names(mdf) <- c("car_3", "car_4")
    merge(df, mdf, by.x = c("car_1", "car_2"), by.y = c("car_3", "car_4"), 
          all.x = FALSE, all.y = TRUE)
    
    0 讨论(0)
  • 2021-01-11 18:03

    In case you would use match or %in% on multiple columns you could use interaction, paste or use a list to match on multiple columns.

    df[match(interaction(mdf), interaction(df[c("car_1", "car_2")])),]
    
    df[match(paste(mdf$car_1, mdf$car_2), paste(df$car_1, df$car_2),),]
    
    df[match(asplit(mdf, 1), asplit(df[c("car_1", "car_2")], 1)),]
    
    df[interaction(df[c("car_1", "car_2")]) %in% interaction(mdf),]
    
    0 讨论(0)
提交回复
热议问题