Robert's Data Science Blog

Join multiple tibbles

library(magrittr)

The dplyr package has functions for joining two tibbles/dataframes. But what if we want to join more than two tibbles?

Consider the following three tibbles:

A <- tibble::tibble(a = 1:3, id = c("a", "b", "c"))
B <- tibble::tibble(b = 4:6, id = c("a", "b", "c"))
C <- tibble::tibble(c = 7:9, id = c("a", "b", "c"))

We can of course use a join function multiple times:

dplyr::left_join(A, B, by = "id") %>% dplyr::left_join(C, by = "id")
## # A tibble: 3 x 4
##       a id        b     c
##   <int> <chr> <int> <int>
## 1     1 a         4     7
## 2     2 b         5     8
## 3     3 c         6     9

However, this becomes cumbersome as the number of tibbles grow. Consider instead the reduce function from the purrr package:

purrr::reduce(list(A, B, C), dplyr::left_join, by = "id")
## # A tibble: 3 x 4
##       a id        b     c
##   <int> <chr> <int> <int>
## 1     1 a         4     7
## 2     2 b         5     8
## 3     3 c         6     9