How to check if a vector contains repeating elements?

how do you check if a vector contains repeating elements in R?

+5
source share
4 answers

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

a=c(3,5,7,2,7,9)
b=1:10
any(duplicated(a)) #True
any(duplicated(b)) #False
+17
source

Also try to rle(x)find the run lengths of the same values ​​in x.

+3
source

, diff.

a <- 1:10
b <- c(1:5, 5, 7, 8, 9, 10)
diff(a)
diff(b)

:

length(a) == length(unique(a))
length(b) == length(unique(b))
+2

:

> all(diff(c(1,2,3)))
[1] TRUE
Warning message:
In all(diff(c(1, 2, 3))) : coercing argument of type 'double' to logical
> all(diff(c(1,2,2,3)))
[1] FALSE
Warning message:
In all(diff(sort(c(1, 2, 4, 2, 3)))) : coercing argument of type 'double' to logical

, .

0

All Articles