vswitch: Element Selection from a List of Alternatives

Description Usage Arguments Details Value Author(s) See Also Examples

View source: R/vswitch.R

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

1
2
3
4
5
6
7
vswitch(x, ...)
## Default S3 method:
vswitch(x, ...)
## S3 method for class 'factor'
vswitch(x, ...)
## S3 method for class 'character'
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.

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.

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.

Author(s)

Antoine Filipovic Pierucci

See Also

ifelse, switch.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
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)

rmngb documentation built on May 29, 2017, 9:22 p.m.

Related to vswitch in rmngb...