Search code examples
rduplicate-detection

How to find duplicates including the first occurrence


I have this vector

vector <- c("www.one","www.two","www.one","www.three")

and I want to find all duplicates, including the first occurrence of the duplicated value. If I do

dup <- duplicated(vector)

I get

dup
# [1] FALSE FALSE  TRUE FALSE

while I need to get

# [1] TRUE FALSE  TRUE FALSE

Solution

  • You can call duplicated twice, looking for duplicates from the front and from the back.

    duplicated(vector) | duplicated(vector, fromLast=TRUE)
    # [1]  TRUE FALSE  TRUE FALSE