ifelse {base}R Documentation

Conditional Element Selection

Description

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.

Usage

ifelse(test, yes, no)

Arguments

test a logical vector
yes return values for true elements of test.
no return values for false elements of test.

See Also

if.

Examples

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)

[Package Contents]