if I do the following:
x <- c(TRUE, TRUE, FALSE)
if(x) {
print("hey there")
}
x is evaluated as TRUE because the first element is TRUE. I would like the condition to evaluate to TRUE only if every element of x is TRUE. I think there must be an easy way that I’m missing (I have searched). I thought that all.equal would be what I wanted (so I could check that “all elements of x are equal to TRUE“), but it serves a different purpose.
I know it’s not ideal (it doesn’t even check to see if x is a logical), but the best I’ve come up with so far is to do something like this:
xu_if <- function(x) {
sum(x) == length(x)
}
if(xu_if(x)) {
print("hey there")
}
What is the best way to do this?
all()is the function you are looking for