Identifies all entries with replicated values, that is, with values that exist more than once.

isReplicated(x, ...)
 replicates(x, ...)

Arguments

x

A vector of length K.

...

Additional arguments passed to duplicated().

Value

A logical

vector of length K, where TRUE indicates that the value exists elsewhere, otherwise not.

Details

Let reps <- isReplicated(x). Then it always holds that:

  • reps == rev(isReplicated(rev(x)))

  • reps == duplicated(x) | duplicated(x, fromLast=TRUE)

  • reps == !is.element(x, setdiff(x, unique(x[duplicated(x)])))

Author

Henrik Bengtsson

See also

Internally duplicated() is used. See also isSingle().

Examples


x <- c(1,1,2,3,4,2,1)
x <- base::letters[x]
print(x)
#> [1] "a" "a" "b" "c" "d" "b" "a"

# Identify entries with replicated values
reps <- isReplicated(x)
print(x[reps])
#> [1] "a" "a" "b" "b" "a"
stopifnot(x[reps] == replicates(x))

# Identify entries with unique values
print(x[!reps])
#> [1] "c" "d"
stopifnot(x[!reps] == singles(x))


# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Validation
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
x <- c(1,1,2,3,4,2,1)
x <- base::letters[x]
reps <- isReplicated(x)

stopifnot(all(table(x[reps]) > 1))
stopifnot(all(table(x[!reps]) == 1))
stopifnot(all(reps == rev(isReplicated(rev(x)))))
stopifnot(all(reps == duplicated(x) | duplicated(x, fromLast=TRUE)))
stopifnot(all(reps == !is.element(x, setdiff(x, unique(x[duplicated(x)])))))
stopifnot(all(sort(c(singles(x), replicates(x))) == sort(x)))


# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Benchmarking singles()
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
set.seed(0xBEEF)
n <- 1e6
x <- sample(1:(n/2), size=n, replace=TRUE)
t <- system.time({
  s <- isSingle(x)
})
print(sum(s))
#> [1] 135603

t0 <- system.time({
  s0 <- !(x %in% x[duplicated(x)]);
})
print(t/t0)
#>      user    system   elapsed 
#> 0.9444444       Inf 1.1351351 
stopifnot(all(s == s0))