%?% {bazar} | R Documentation |
This is a C like ternary operator, the syntax being
condition %?% true %:% false
.
condition %?% true
lhs %:% false
condition |
logical. A vector. |
true , false |
Values to use for |
lhs |
Left-hand side of |
If length(x) > 1
, then ifelse
is used.
Richie Cotton, see https://stackoverflow.com/a/8791496/3902976; Paul Poncet for the small modifications introduced.
(capitalize <- sample(c(TRUE, FALSE), 1))
capitalize %?% LETTERS[1:3] %:% letters[1:2]
# Does not work
## Not run:
capitalize %?% 1*1:3 %:% 1:2
## End(Not run)
# Does work
capitalize %?% {1*1:3} %:% 1:2
# Does work too
capitalize %?% (1*1:3) %:% 1:2
# Vectorized version also works
c(capitalize,!capitalize) %?% "A" %:% c("b","c")
# Chaining operators is permitted
FALSE %?% "a" %:%
(FALSE %?% "b") %:%
(capitalize %?% "C") %:% "c"