Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 462
CptNemo Avatar asked Sep 25 '13 06:09

CptNemo


2 Answers

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
like image 170
Vincent Zoonekynd Avatar answered Oct 20 '22 00:10

Vincent Zoonekynd


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
like image 20
Carl Witthoft Avatar answered Oct 20 '22 00:10

Carl Witthoft