如何在 R 中使用 dplyr 执行多个左连接 [英] How to perform multiple left joins using dplyr in R
本文介绍了如何在 R 中使用 dplyr 执行多个左连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何使用 dplyr
在 R 中加入多个数据帧?
How do I join multiple dataframes in R using dplyr
?
new <- left_join(x,y, by = "Flag")
这是我用来左连接 x 和 y 的代码该代码不适用于多个连接
this is the code I am using to left join x and y the code doesn't work for multiple joins
new <- left_join(x,y,z by = "Flag")
推荐答案
可以使用嵌套的left_join
left_join(x, y, by='Flag') %>%
left_join(., z, by='Flag')
或者另一种选择是将所有数据集放在 list
中,并使用 merge
从 base R
和 Reduce
>
Or another option would to place all the datasets in a list
and use merge
from base R
with Reduce
Reduce(function(...) merge(..., by='Flag', all.x=TRUE), list(x,y,z))
或者我们有来自 plyr
的 join_all
.在这里,我们也将数据帧放在 list
中,并使用参数 type='left'
进行左连接.
Or we have join_all
from plyr
. Here also, we place the dataframes in a list
and use the argument type='left'
for left join.
library(plyr)
join_all(list(x,y,z), by='Flag', type='left')
这篇关于如何在 R 中使用 dplyr 执行多个左连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文