I am trying to write an if statement
that checks if one OR another categorical variable is within a column in my dataframe. Therefore, I am using %in%
. It works perfectly fine when I have 1 variable:
if("setosa" %in% iris$Species){
print("hi")
}
[1] "hi"
But I cannot use it if I have a condition OR
.
# it should return TRUE because "setosa" is within the column Species
if(("setosa" | "new") %in% iris$Species){
print("hi")
}
Error in "setosa" | "virginica" :
operations are possible only for numeric, logical or complex types
Does anybody know how to do it or if I can use another function to check if my if statement
is TRUE or FALSE?
Thanks in advance
CodePudding user response:
Put the conditions in a vector and check if any is true (for OR, use all
for AND) any(c("setosa","new") %in% iris$Species)
.