Learn R Programming

rmngb (version 0.6-1)

vswitch: Element Selection from a List of Alternatives

Description

vswitch returns a value with the same shape as x which is filled with elements selected from... depending on the values of x.

Usage

vswitch(x, ...) "vswitch"(x, ...) "vswitch"(x, ...) "vswitch"(x, ...)

Arguments

x
integer, factor or character. Which element of ... should be selected.
...
vectors. If x is character or factor elements in ... should be named. If x is integer names will be ignored with a warning.

Value

A vector of the same length as x and a class depending on the result of the coercion of all the elements in ... to the same class.

Details

The class of the return value depends on the class(es) of .... All elements in ... are coerced to the same class with the usual rules.

If x is character or factor then ... should be named. All values of x should correspond to names in ....

If x is integer then names in ... will be ignored with a warning. Furthermore if values of x are smaller than 1 or greater than the number of elements in ... an error will be produced.

NA values in x return NA.

See Also

ifelse, switch.

Examples

Run this code
a <- 1:5
b <- 11:15
c <- 21:25

condChar <- c('d', 'e', 'f', 'd', 'e')
condInt <- c(1, 2, 3, 1, 2)
condFac <- as.factor(condChar)
condMissing <- c(1, NA, 3, NA, 2)

vswitch(x = condChar, d = a, e = b, f = c)
vswitch(x = condInt, a, b, c)
vswitch(x = condFac, d = a, e = b, f = c)
vswitch(x = condMissing, a, b, c)

Run the code above in your browser using DataLab