all_identical: Test if all items in a vector or a list are identical.

View source: R/misc.utilities.R

all_identicalR Documentation

Test if all items in a vector or a list are identical.

Description

Test if all items in a vector or a list are identical.

Usage

all_identical(x, .p = identical, .ref = 1L, ...)

Arguments

x

a vector or a list

.p

a predicate function of two arguments returning a logical. Defaults to identical().

.ref

integer; index of element of x to which all the remaining ones will be compared. Defaults to 1.

...

additional arguments passed to .p()

Value

By default TRUE if all elements of x are identical to each other, FALSE otherwise. In the general case, all_identical() returns TRUE if and only if .p() returns TRUE for all the pairs involving the first element and the remaining elements.

See Also

identical(), all.equal()

Examples


stopifnot(!all_identical(1:3))

stopifnot(all_identical(list("a", "a", "a")))

# Using with `all.equal()` has its quirks 
# because of numerical tolerance:
x <- seq(
  .Machine$double.eps, 
  .Machine$double.eps + 1.1 * sqrt(.Machine$double.eps), 
  length = 3
)
# Results with `all.equal()` are affected by ordering
all_identical(x, all.equal) # FALSE
all_identical(x[c(2,3,1)], all.equal) # TRUE
# ... because `all.equal()` is intransitive
all_identical(x[-3], all.equal) # is TRUE and
all_identical(x[-1], all.equal) # is TRUE, but
all_identical(x[-2], all.equal) # is FALSE


statnet/statnet.common documentation built on June 9, 2025, 2:34 a.m.