\Header{ifelse}{Conditional Element Selection}
\keyword{logic}{ifelse}
\keyword{programming}{ifelse}
\begin{Description}\relax
\code{ifelse} returns a value with the same shape as
\code{test} which is filled with elements selected
from either \code{yes} or \code{no}
depending on whether the element of \code{test}
is \code{TRUE} or \code{FALSE}.
\end{Description}
\begin{Usage}
\begin{verbatim}
ifelse(test, yes, no)
\end{verbatim}
\end{Usage}
\begin{Arguments}
\begin{ldescription}
\item[\code{test}] an object which can be coerced to logical mode.
\item[\code{yes}] return values for true elements of \code{test}.
\item[\code{no}] return values for false elements of \code{test}.
\end{ldescription}
\end{Arguments}
\begin{Details}\relax
If \code{yes} or \code{no} are too short, their elements are recycled.
\code{yes} will be evaluated if and only if any element of \code{test}
is true, and analogously for \code{no}.

Missing values in \code{test} giving missing values in the result.
\end{Details}
\begin{Value}
A vector of the same length and attributes (including
class) as \code{test} and data values from the values of
\code{yes} or \code{no}.  The mode of the answer will be coerced to
logical to accommodate first any values taken from \code{yes} and then
any values taken from \code{no}.
\end{Value}
\begin{Section}{Warning}
The mode of the result may depend on the value of \code{test}, and the
class attribute of the result is taken from \code{test} and may be
inappropriate for the values selected from \code{yes} and \code{no}.

Sometimes it is better to use a construction such as
\code{(tmp <- yes; tmp[!test] <- no[!test]; tmp)}, possibly extended
to handle missing values in \code{test}.
\end{Section}
\begin{References}\relax
Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988)
\emph{The New S Language}.
Wadsworth \& Brooks/Cole.
\end{References}
\begin{SeeAlso}\relax
\code{\Link{if}}.
\end{SeeAlso}
\begin{Examples}
\begin{ExampleCode}
x <- c(6:-4)
sqrt(x)#- gives warning
sqrt(ifelse(x >= 0, x, NA))# no warning

## Note: the following also gives the warning !
ifelse(x >= 0, sqrt(x), NA)
\end{ExampleCode}
\end{Examples}

