mardi 19 février 2019

Multiple arguments in if-else from vector [duplicate]

This question already has an answer here:

I am trying to call multiple conditions to an if statement from a vector.

Say I have the following code:

object = "tree"

if(object == "tree" | object == "bush" | object == "grass"){
    print("Failure!?")

} else {
    print("Success!")

However instead of typing "tree", "bush", and "grass", I would like to call the arguments from a vector. e.g

 conditions <- c("tree", "bush", "grass")

However, the following doesn't work (obviously):

if(object == conditions){
    print("Failure!?")

} else {
    print("Success!")

Many thanks for any suggestions!

Aucun commentaire:

Enregistrer un commentaire