is.constant <- function(x)
{
## Purpose: Check if a vector's values are all the same.
## Arguments:
## x: a vector (numeric, character, logical, etc.)
## Return: a logical value tha is TRUE if and only if all the values are the same (constant).
## Author: Feiming Chen, Date: 13 Apr 2017, 14:59
## ________________________________________________
y <- rep.int(x[1], length(x))
isTRUE(all.equal(x, y))
}
if (F) { # Unit Test
is.constant(c(T, T, F)) # F
is.constant(c(F, F, F)) # T
is.constant(c(1, 1, 2)) # F
is.constant(c(2, 2, 2)) # T
is.constant(c("a", "a", "b")) # F
is.constant(c("b", "b", "b")) # T
}
Thursday, April 13, 2017
Check if a vector's values are all the same
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment