Test for equality of all vector elements
Lets try 4 methods: Using https://www.geeksforgeeks.org/r-language/test-for-equality-of-all-vector-elements-in-r/ as reference Clearly the best solution is all(x[1] == x) or any(x[1] != x) > library(microbenchmark) > x <- runif(10000000) > microbenchmark({all(x[1] == x)}, {any(x[1] != x)}, + {length(unique(x)) != 1}, {var(x) != 0}) Unit: milliseconds expr min lq mean median uq max neval { all(x[1] == x) } 9.196095 9.388385 11.34733 9.563066 10.537984 119.53070 100 { ...