Programming
r dataframe merge dplyr
Updated Thu, 25 Aug 2022 09:46:32 GMT

How to join two dataframes with dplyr based on two columns with different names in each dataframe?


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"))



Solution

df3 <- dplyr::left_join(df1, df2, by=c("name1" = "name3", "name2" = "name4"))




Comments (1)