如何在R中使用dplyr执行多个左连接 [英] How to perform multiple left joins using dplyr in R

查看:289
本文介绍了如何在R中使用dplyr执行多个左连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用 dplyr 加入R中的多个数据框?

  new<  -  left_join(x,y,by =Flag)

这是代码我正在使用left x和y
代码不适用于多个连接

  new<  - left_join(x,y,z by =Flag)


解决方案

p>您可以使用嵌套的 left_join

  left_join(x,y, by ='Flag')%>%
left_join(。,z,by ='Flag')

或者另一个选项是将所有数据集放在列表中,并使用<$ c c中的 merge $ c> base R with 减少

  Reduce(function(...)merge(...,by ='Flag',all.x = TRUE),list(x,y,z))
pre>

或者我们从 plyr 中有 join_all 。此处,我们将数据框放在列表中,并使用参数 type ='left'作为左连接。 / p>

  library(plyr)
join_all(list(x,y,z),by ='Flag',type = 'left')


How do I join multiple dataframes in R using dplyr ?

new <- left_join(x,y, by = "Flag")

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

解决方案

You can use nested left_join

 left_join(x, y, by='Flag') %>%
                left_join(., z, by='Flag') 

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

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆