8

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
CptNemo
  • 6,455
  • 16
  • 58
  • 107

2 Answers2

12

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
Vincent Zoonekynd
  • 31,893
  • 5
  • 69
  • 78
3

Here's another way:

Rgames> foo<-c('a','b','d','f','a','b','b','q')
Rgames> which(foo%in%foo[which(duplicated(foo))])
[1] 1 2 5 6 7
Carl Witthoft
  • 20,573
  • 9
  • 43
  • 73