As the question asks, is there a control sequence in R similar to C's ternary operator? If so, how do you use it? Thanks!
R – Does the Ternary Operator Exist?
operatorsr
operatorsr
As the question asks, is there a control sequence in R similar to C's ternary operator? If so, how do you use it? Thanks!
Best Answer
As
if
is function inR
and returns the latest evaluation, if-else is equivalent to?:
.The power of R is vectorization. The vectorization of the ternary operator is
ifelse
:Just kidding, you can define c-style
?:
:here, you don't need to take care about brackets:
but you need brackets for assignment :(
Finally, you can do very similar way with c:
You can get rid of brackets:
These are not for daily use, but maybe good for learning some internals of R language.