Test for equality among all elements of a single numeric vector

Why not simply using the variance:

var(x) == 0

If all the elements of x are equal, you will get a variance of 0.
This works only for double and integers though.

Edit based on the comments below:
A more generic option would be to check for the length of unique elements in the vector which must be 1 in this case. This has the advantage that it works with all classes beyond just double and integer from which variance can be calculated from.

length(unique(x)) == 1

Leave a Comment