Test for equality among all elements of a single vector

kmm picture kmm · Jan 20, 2011 · Viewed 74.2k times · Source

I'm trying to test whether all elements of a vector are equal to one another. The solutions I have come up with seem somewhat roundabout, both involving checking length().

x <- c(1, 2, 3, 4, 5, 6, 1)  # FALSE
y <- rep(2, times = 7)       # TRUE

With unique():

length(unique(x)) == 1
length(unique(y)) == 1

With rle():

length(rle(x)$values) == 1
length(rle(y)$values) == 1

A solution that would let me include a tolerance value for assessing 'equality' among elements would be ideal to avoid FAQ 7.31 issues.

Is there a built-in function for type of test that I have completely overlooked? identical() and all.equal() compare two R objects, so they won't work here.

Edit 1

Here are some benchmarking results. Using the code:

library(rbenchmark)

John <- function() all( abs(x - mean(x)) < .Machine$double.eps ^ 0.5 )
DWin <- function() {diff(range(x)) < .Machine$double.eps ^ 0.5}
zero_range <- function() {
  if (length(x) == 1) return(TRUE)
  x <- range(x) / mean(x)
  isTRUE(all.equal(x[1], x[2], tolerance = .Machine$double.eps ^ 0.5))
}

x <- runif(500000);

benchmark(John(), DWin(), zero_range(),
  columns=c("test", "replications", "elapsed", "relative"),
  order="relative", replications = 10000)

With the results:

          test replications elapsed relative
2       DWin()        10000 109.415 1.000000
3 zero_range()        10000 126.912 1.159914
1       John()        10000 208.463 1.905251

So it looks like diff(range(x)) < .Machine$double.eps ^ 0.5 is fastest.

Answer

Yohan Obadia picture Yohan Obadia · Mar 9, 2016

Why not simply using the variance:

var(x) == 0

If all the elements of x are equal, you will get a variance of 0.