R: Remove duplicate rows in a data frame – Snippet #2

Discover how to remove duplicate rows in a data frame with R

Packages

This snippet requires dplyr.

With the Tidyverse:

library(tidyverse)

Without the Tidyverse:

library(dplyr)

Code

To remove duplicate rows in a data frame, use distinct(). Duplicate rows are rows that are perfectly identical.

With the pipe operator:

new_df <- df %>%
  distinct()

Without the pipe operator:

new_df <- distinct(df)

The code above removes all perfectly identical rows in df.

df
new_df

Resources

Keep distinct/unique rows — distinct
Keep only unique/distinct rows from a data frame. This is similar to unique.data.frame() but considerably faster.

Read more