dplyr::full_join()
get_help() docs
The full_join() function is part of the {dplyr} package, which is part of the {tidyverse}.
We use this function to merge two relational datasets. Specifically, full_join() retains all rows from both the left and right tibbles (data frames). Missing row information becomes NA in the final output, represented in the GIF below by the blank cells.
To use this function, you need to either first load the {dplyr} library, or always use the function with dplyr::full_join() notation.
# Load the library
library(dplyr)
# Or, load the full tidyverse:
library(tidyverse)
# Or, use :: notation
dplyr::full_join()full_join(left tibble, right tibble)
# is equivalent to:
full_join(right tibble, left tibble)
# or with piping:
left tibble %>%
full_join(right tibble)
# is equivalent to:
right tibble %>%
full_join(left tibble)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# full_join to merge tibbles together.
# Left/right order does not matter except to determine final row and column order
first_tibble %>%
full_join(second_tibble)## # A tibble: 5 × 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>
## 5 Sheep herbi <NA> Artiodactyla
# full_join to merge tibbles together.
# Left/right order does not matter except to determine final row and column order
second_tibble %>%
full_join(first_tibble)## # A tibble: 5 × 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>
## 5 Tiger carni <NA> en