1

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
  • 77,147
  • 31
  • 174
  • 185
wake_wake
  • 1,242
  • 2
  • 18
  • 44

1 Answers1

15
df3 <- dplyr::left_join(df1, df2, by=c("name1" = "name3", "name2" = "name4"))
amarchin
  • 1,974
  • 1
  • 13
  • 30