Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
129 views
in Technique[技术] by (71.8m points)

r - else if(){} VS ifelse()

Why can we use ifelse() but not else if(){} in with() or within() statement ?

I heard the first is vectorizable and not the latter. What does it mean ?

Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...