如何使用dplyr基于每个数据帧中具有不同名称的两列来连接两个数据帧? [重复]

时间:2022-06-16 04:14:23

This question already has an answer here:

这个问题在这里已有答案:

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?

如何使用dplyr基于每个data.frame中具有不同名称的两列来连接两个data.frames?

With base::merge one can simply merge:

使用base :: 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.

其中df1 $ name1 == df2 $ name3和df1 $ name2 == df2 $ name4。

How does one do this in dplyr?

如何在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:

我知道可以使用dplyr中的by函数来连接两个data.frames,基于一个具有不同名称的列:

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

1 个解决方案

#1


4  

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

#1


4  

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