Usage

Arguments

Details

If yes or no are too short, their elements are recycled.
yes will be evaluated if and only if any element of test
is true, and analogously for no.

Missing values in test give missing values in the result.

Value

A vector of the same length and attributes (including dimensions and
"class") as test and data values from the values of
yes or no. The mode of the answer will be coerced from
logical to accommodate first any values taken from yes and then
any values taken from no.

Warning

The mode of the result may depend on the value of test (see the
examples), and the class attribute (see oldClass) of the
result is taken from test and may be inappropriate for the
values selected from yes and no.

Sometimes it is better to use a construction such as

(tmp <- yes; tmp[!test] <- no[!test]; tmp)

, possibly extended to handle missing values in test.

Further note that if(test) yes else no is much more efficient
and often much preferable to ifelse(test, yes, no) whenever
test is a simple true/false result, i.e., when
length(test) == 1.

The srcref attribute of functions is handled specially: if
test is a simple true result and yes evaluates to a function
with srcref attribute, ifelse returns yes including
its attribute (the same applies to a false test and no
argument). This functionality is only for backwards compatibility, the
form if(test) yes else no should be used whenever yes and
no are functions.