How to perform multiple left joins using dplyr in R [duplicate]
You can use nested left_join
library(dplyr)
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')
As @JBGruber mentioned in the comments, it can also be done via purrr
library(purrr)
library(dplyr)
purrr::reduce(list(x,y,z), dplyr::left_join, by = 'Flag')