Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to perform multiple left joins using dplyr in R [duplicate]

Tags:

r

dplyr

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") 
like image 979
pranav Avatar asked Aug 18 '15 07:08

pranav


People also ask

How do I use left join in dplyr?

Joins with dplyr. dplyr uses SQL database syntax for its join functions. A left join means: Include everything on the left (what was the x data frame in merge() ) and all rows that match from the right (y) data frame. If the join columns have the same name, all you need is left_join(x, y) .

Why do I have more rows after left join in R?

If one of the tables in the LEFT JOIN has more than one corresponding value, it will create a new row. If you don't want this behaviour, you need to use an aggregating function and GROUP BY .


1 Answers

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') 
like image 152
akrun Avatar answered Sep 29 '22 18:09

akrun