3

This is a really simple question, but can't find a suitable answer here.

How does one join two data.frames with dplyr based on two columns with different names in each data.frame?

With base::merge one can simply merge:

df3 <- merge(df1, df2, by.x=c("name1", "name2"), by.y=c("name3", "name4"))

where df1$name1 == df2$name3 and df1$name2 == df2$name4.

How does one do this in dplyr?


I know that one can use the by function in dplyr to do join two data.frames with based on one column with a different name:

df3 <- dplyr::left_join(df1, df2, by=c("name1" = "name3"))
Jaap
  • 81,064
  • 34
  • 182
  • 193
wake_wake
  • 1,332
  • 2
  • 19
  • 46

1 Answers1

18
df3 <- dplyr::left_join(df1, df2, by=c("name1" = "name3", "name2" = "name4"))
amarchin
  • 2,044
  • 1
  • 16
  • 32