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!
Your link points to an if
statement.
> x <- 1
> if(x < 2) print("Less than") else print("Greater than")
[1] "Less than"
If your input variable is a vector, then ifelse
might be more suitable:
> x <- 1:3
> ifelse(x<=2, "Less than or equal", "Greater than")
[1] "Less than or equal" "Less than or equal" "Greater than"
To access the help page for if
, you need to embed the if
in backticks:
?`if`
The help page for ifelse
is at:
`?ifelse`