pick: Conditional Element Selection

View source: R/pick.R

pickR Documentation

Conditional Element Selection

Description

Return the value associated with test from the supplied vectors.

Usage

pick(test, ..., .pass = test, na = NA)

Arguments

test

a logical, numeric, or character vector that indicates which value to select from the data supplied in ... See Details.

...

the values to be selected.

.pass

the value to return for any element of test that does not match an argument name in ... Useful only when the class of test is "character" or "factor."

na

the value to return for any element of test is NA.

Details

If test is logical, then if test is TRUE, return the first argument in ..., otherwise return the second argument.
If test is numeric, then return that value in the list defined by ...
If test is character, then return that value in the list defined by ..., which must be named in the call.
If test is NA, then return the value specified by na.

Value

A vector of the same length as test and data values from the values list defined by ... The mode of the result will be coerced from the values list defined by ...

Note

This function is designed to replace nested ifelse expressions. See Examples. It is different from switch in that the value selected from the possible alternatives is selected by the values in test rather than by a single value.

See Also

ifelse, switch

Examples


## Create the test vector
testpick <- c(1,2,3,1)
## Nested ifelse
ifelse(testpick == 1, 1,
 ifelse(testpick == 2, 3,
   ifelse(testpick == 3, 5, NA)))
## Results by pick:
pick(testpick, 1, 3, 5)
## Create a test vector of character data
testpick <- c("a","b","c","a")
pick(testpick, a=1, b=3, c=5)


USGS-R/smwrBase documentation built on Oct. 18, 2022, 9:55 a.m.