ifelse(test, yes, no)
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. |
# 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