dplyr::inner_join()
   get_help() docs


Description

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

We use this function to merge two relational datasets to retain only information that is shared by both tibbles (data frames). Specifically, inner_join() retains only rows that are present in both the left and right tibbles (data frames):

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

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

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

Conceptual Usage

inner_join(left tibble, right tibble)
# is equivalent to:
inner_join(right tibble, left tibble)


# or with piping:
left tibble %>%
  inner_join(right tibble)
# is equivalent to:
right tibble %>%
  inner_join(left 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


# inner_join first_tibble and second_tibble
# Left/right order does not matter except to determine final column order
first_tibble %>%
  inner_join(second_tibble)
## # A tibble: 3 × 4
##   name   vore  conservation order       
##   <chr>  <chr> <chr>        <chr>       
## 1 Dog    carni domesticated Carnivora   
## 2 Pig    omni  domesticated Artiodactyla
## 3 Rabbit herbi domesticated Lagomorpha


# inner_join first_tibble and second_tibble
# Left/right order does not matter except to determine final column order
second_tibble %>%
  inner_join(first_tibble)
## # A tibble: 3 × 4
##   name   vore  order        conservation
##   <chr>  <chr> <chr>        <chr>       
## 1 Dog    carni Carnivora    domesticated
## 2 Pig    omni  Artiodactyla domesticated
## 3 Rabbit herbi Lagomorpha   domesticated