Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to test whether a vector contains repetitive elements?

Tags:

r

vector

how do you test whether a vector contains repetitive elements in R?

like image 506
ZNN Avatar asked Mar 10 '11 17:03

ZNN


People also ask

How do you know if a vector is unique?

Proof (a) Suppose that 0 and 0 are both zero vectors in V . Then x + 0 = x and x + 0 = x, for all x ∈ V . Therefore, 0 = 0 + 0, as 0 is a zero vector, = 0 + 0 , by commutativity, = 0, as 0 is a zero vector. Hence, 0 = 0 , showing that the zero vector is unique.

Can std :: vector have duplicates?

No, it is only bad if the source data is particularly large and unlikely to have any duplicates.


2 Answers

I think I found the answer. Use duplicated() function:

a=c(3,5,7,2,7,9)
b=1:10
any(duplicated(a)) #True
any(duplicated(b)) #False
like image 80
ZNN Avatar answered Sep 16 '22 22:09

ZNN


Also try rle(x) to find the lengths of runs of identical values in x.

like image 35
J. Win. Avatar answered Sep 20 '22 22:09

J. Win.