Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Ternary conditional operator
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
===R=== The traditional if-else construct in [[R (programming language)|R]] (which is an implementation of [[S (programming language)|S]]) is: <syntaxhighlight lang="r"> if (a < b) { x <- "true" } else { x <- "false" } </syntaxhighlight> If there is only one statement in each block, braces can be omitted, like in [[C (programming language)|C]]: <syntaxhighlight lang="r"> if (a < b) x <- "true" else x <- "false" </syntaxhighlight> The code above can be written in the following non-standard condensed way: <syntaxhighlight lang="r"> x <- if (a < b) "true" else "false" </syntaxhighlight> There exists also the function {{code|ifelse}} that allows rewriting the expression above as: <syntaxhighlight lang="r"> x <- ifelse(a < b, "true", "false") </syntaxhighlight> The {{code|ifelse}} function is automatically vectorized. For instance: <syntaxhighlight lang="rconsole"> > ifelse(c (0, 2) < 1, "true", "false") [1] "true" "false" </syntaxhighlight>
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)