ifelse(test, yes, no)
ifelse
returns a value with the same shape as
test
which is filled with elements selected
from either yes
or no
depending on whether the element of test
is TRUE
or FALSE
.
If yes
or no
are too short, their elements are recycled.if
.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)