dplyr::left_join()
   get_help() docs


Description

The left_join() function is part of the {dplyr} package, which is part of the {tidyverse}.

We use this function to merge two relational datasets. Specifically, left_join() retains all rows from the left tibble (data frame) and incorporates columns from the right tibble (data frame): It merges the right into the left. Missing row information becomes NA in the final output, represented in the GIF below by the blank cell.

To use this function, you need to either first load the {dplyr} library, or always use the function with dplyr::left_join() notation.

# Load the library
library(dplyr)
# Or, load the full tidyverse:
library(tidyverse)

# Or, use :: notation
dplyr::left_join()

Conceptual Usage

left_join(left tibble, right tibble)

# or with piping:
left tibble %>%
  left_join(right tibble)

Examples

Consider the following example datasets, These two tibbles have column names name and vore in common. They both contain rows for “Dog”, “Pig”, and “Rabbit”, but first_tibble also contains “Tiger” and second_tibble also contains “Sheep”.

first_tibble
## # A tibble: 4 × 3
##   name   vore  conservation
##   <chr>  <chr> <chr>       
## 1 Dog    carni domesticated
## 2 Pig    omni  domesticated
## 3 Rabbit herbi domesticated
## 4 Tiger  carni en

second_tibble
## # A tibble: 4 × 3
##   name   vore  order       
##   <chr>  <chr> <chr>       
## 1 Dog    carni Carnivora   
## 2 Pig    omni  Artiodactyla
## 3 Rabbit herbi Lagomorpha  
## 4 Sheep  herbi Artiodactyla


# left_join to merge second_tibble into first_tibble
first_tibble %>%
  left_join(second_tibble)
## # A tibble: 4 × 4
##   name   vore  conservation order       
##   <chr>  <chr> <chr>        <chr>       
## 1 Dog    carni domesticated Carnivora   
## 2 Pig    omni  domesticated Artiodactyla
## 3 Rabbit herbi domesticated Lagomorpha  
## 4 Tiger  carni en           <NA>


# left_join to merge first_tibble into second_tibble
second_tibble %>%
  left_join(first_tibble)
## # A tibble: 4 × 4
##   name   vore  order        conservation
##   <chr>  <chr> <chr>        <chr>       
## 1 Dog    carni Carnivora    domesticated
## 2 Pig    omni  Artiodactyla domesticated
## 3 Rabbit herbi Lagomorpha   domesticated
## 4 Sheep  herbi Artiodactyla <NA>