ifelse
Conditional Data Selection

Description

Places values into an object like test, according to the logical values in test.

Usage

ifelse(test, yes, no)

Arguments

test a logical object. Missing values (NA) are allowed.
yes a vector containing the values to be returned for elements with test equal to TRUE.
no a vector containing the values to be returned for elements with test equal to FALSE.

Details

NA values in test cause NAs in the result.
Value
returns an object with the attributes of test and the data values from the values of yes or no, depending on test. If yes or no are not as long as test, they are repeated cyclically.
See Also
if, ifelse1, Logic, switch.
Examples
# Randomly mix two vectors
x <- letters[1:5]  # shorter vector
y <- LETTERS[1:20] # longer vector 
ifelse(runif(10) < 0.5, x, y)
# result is of length 10

# Note that this only returns the FIRST # element of x or y since cond is of length 1! cond <- FALSE ifelse(cond, x, y) # [1] "A"

# This works as expected if (cond) x else y

Package base version 6.0.0-69
Package Index