dplyr::anti_join()
   get_help() docs


Description

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

We use this function to merge two relational datasets and retain only information that is not shared between them. In order to merge relational datasets, there must be at least one column name in common.

Specifically, anti_join() retains all rows from the left tibble (data frame) which are not present in the right tibble (data frame):

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

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

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

Conceptual Usage

anti_join(left tibble, right tibble)

# or with piping:
left tibble %>%
  anti_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


# anti_join with first_tibble on the left and second_tibble on the right
first_tibble %>%
  anti_join(second_tibble)
## # A tibble: 1 × 3
##   name  vore  conservation
##   <chr> <chr> <chr>       
## 1 Tiger carni en


# anti_join with second_tibble on the left and first_tibble on the right
second_tibble %>%
  anti_join(first_tibble)
## # A tibble: 1 × 3
##   name  vore  order       
##   <chr> <chr> <chr>       
## 1 Sheep herbi Artiodactyla