duplicated()
duplicated(x, incomparables = FALSE, MARGIN = 1, fromLast = FALSE, ...) Returns:
logical vector · Updated March 13, 2026 · Base Functions vector subsetting base deduplication
duplicated() returns a logical vector indicating which elements (or rows) are duplicates. This function is fundamental for data cleaning and analysis.
Syntax
duplicated(x, incomparables = FALSE, MARGIN = 1, fromLast = FALSE, ...)
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
x | vector/data.frame/array | — | Input object to process |
incomparables | vector | FALSE | Values to treat as incomparable |
MARGIN | integer | 1 | For arrays: 1 for rows, 2 for columns |
fromLast | logical | FALSE | If TRUE, count from last occurrence |
... | arguments | — | Additional arguments passed to methods |
Examples
Basic usage with vectors
x <- c(1, 2, 2, 3, 3, 3, 4)
duplicated(x)
# [1] FALSE FALSE TRUE FALSE TRUE TRUE FALSE
Working with character vectors
names <- c("Alice", "Bob", "Charlie", "Alice", "Diana", "Bob")
duplicated(names)
# [1] FALSE FALSE FALSE TRUE FALSE TRUE
# Get only unique names
names[!duplicated(names)]
# [1] "Alice" "Bob" "Charlie" "Diana"
With data frames
df <- data.frame(
id = c(1, 2, 2, 3, 4, 1),
name = c("A", "B", "B", "C", "D", "A")
)
duplicated(df)
# [1] FALSE FALSE TRUE FALSE FALSE TRUE
Using fromLast argument
# Check from last occurrence instead of first
x <- c(1, 2, 2, 3)
duplicated(x, fromLast = TRUE)
# [1] TRUE FALSE FALSE FALSE
Common Patterns
Remove duplicates from data frame
# Base R approach - keep first occurrence
df_unique <- df[!duplicated(df), ]
# Using dplyr
library(dplyr)
df_unique <- distinct(df)
Find duplicate indices
# Find first duplicate index
which(duplicated(x))
# [1] 3 4
# Find all duplicates (including repeated)
anyDuplicated(x)
# [1] 3
Filter to keep non-duplicated rows
# Keep only rows that haven't been seen before
df[!duplicated(df), ]