There is a question on SO about this: http://stackoverflow.com/questions/4752275/test-for-equality-among-all-elements-of-a-single-vector But I was a bit more curious, so! #test data, large vectors v1 = rep(1234, 1e6) v2 = runif(1e6) #functions to try all_the_same1 = function(x) { range(x) == 0 } all_the_same2 = function(x) { max(x) == min(x) } all_the_same3 = function(x) { sd(x) == 0 } all_the_same4 = function(x) { var(x) == 0 } all_the_same5 = function(x) { x = x - mean(x) all(x == 0) } all_the_same6 = function(x) { length(unique(x)) == 1 }
R: fastest way of finding out of all elements of a vector are identical?
R: fastest way of finding out of all elements…
R: fastest way of finding out of all elements of a vector are identical?
There is a question on SO about this: http://stackoverflow.com/questions/4752275/test-for-equality-among-all-elements-of-a-single-vector But I was a bit more curious, so! #test data, large vectors v1 = rep(1234, 1e6) v2 = runif(1e6) #functions to try all_the_same1 = function(x) { range(x) == 0 } all_the_same2 = function(x) { max(x) == min(x) } all_the_same3 = function(x) { sd(x) == 0 } all_the_same4 = function(x) { var(x) == 0 } all_the_same5 = function(x) { x = x - mean(x) all(x == 0) } all_the_same6 = function(x) { length(unique(x)) == 1 }