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!
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
As
ifis function inRand 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.