else if(){} VS ifelse()

The if construct only considers the first component when a vector is passed to it, (and gives a warning)

if(sample(100,10)>50) 
    print("first component greater 50") 
else 
    print("first component less/equal 50")

The ifelse function performs the check on each component and returns a vector

ifelse(sample(100,10)>50, "greater 50", "less/equal 50")

The ifelse function is useful for transform, for instance. It is often useful to
use & or | in ifelse conditions and && or || in if.

Leave a Comment