How to test whether a vector contains repetitive elements?
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
Also try rle(x)
to find the lengths of runs of identical values in x
.
If you're looking for consecutive repeats you can use diff
.
a <- 1:10
b <- c(1:5, 5, 7, 8, 9, 10)
diff(a)
diff(b)
Or anywhere in the vector:
length(a) == length(unique(a))
length(b) == length(unique(b))