Join more than two tables

Hi,

I have four tables I would like to join.
When I want to join two tables, I use dplyr and the command

NewDataFrame <-left_join(Table1, Table2, by="SharedColumn")

I tried with four tables

NewDataFrame <-left_join(Table1, Table2, Table3, Table4, by="SharedColumn")

But of course it does not work.

Is there an easy or more clean approach other than combining table1 and table2, then table 3, then table4?

Thank you in advance.

Hi @fgaascht,
Try using purrr like this:

library(tidyverse)
dfs <- list(
  a = data.frame(x = 1:10, a = runif(10)),
  b = data.frame(x = 1:10, b = runif(10)),
  c = data.frame(x = 1:10, c = runif(10)))
purrr::reduce(dfs, left_join) 

HTH

2 Likes

An alternative approach would be to pipe (%>%) the steps together, like this:

library(tidyverse)
NewDataFrame <- left_join(Table1, Table2, by="SharedColumn") %>%
                left_join(Table3, by="SharedColumn") %>%
                left_join(Table4, by="SharedColumn")

The pipe operator %>% takes the result of the first left_join and inputs it into the second, and then into the third left_join.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.