R Language Set operations Make unique / drop duplicates / select distinct elements from a vector


Example

unique drops duplicates so that each element in the result is unique (only appears once):

x = c(2, 1, 1, 2, 1)

unique(x)
# 2 1

Values are returned in the order they first appeared.

duplicated tags each duplicated element:

duplicated(x)
# FALSE FALSE TRUE TRUE TRUE

anyDuplicated(x) > 0L is a quick way of checking whether a vector contains any duplicates.