recode: Flexible vector recoding

Description Usage Arguments Details Value See Also Examples

View source: R/recode.R

Description

Collapse multiple original values of a vector into single new values. An alternative to nested ifelse() statements, recode, and revalue.

Usage

1
2
3
recode(x, ..., .default = NULL, .na = NA, factor = FALSE)

binary_recode(x, one, .na = NA)

Arguments

x

a vector

...

a sequence of arguments where the name is the replacement and the value are the elements to be replaced.

.default

a vector of length 1. The value is assigned to all elements of x not matching any of the replacement values in ... or .na (i.e. a catch-all replacement value for anything not explicitly mapped). If NULL (default), elements of x without a mapping in ... or .na remain the same (i.e. no recoding).

.na

a vector. All values in .na will be assigned NA. Default is that any NA values in x remain NA. If NULL, no values are assigned NA values.

factor

If TRUE, returned the recoded version of x as a factor. Defaults to FALSE, in which all non-NA elements are forced to character.

one

for binary_recode(), all elements of x in one will be assigned 1, anything in .na will be assigned NA, and anything left over will be assigned 0.

Details

Further information on recode()'s primary value mapping arguments:

...

Any number of named arguments in which for each argument, any values occurring in x will be recoded to the name of the argument. There are no defaults in recode(), so recode(x) with no additional arguments should be equivalent to identity(x)

.na

Any values of x matching values in the vector provided to .na will be recoded to NA.

.default

Any values of x not matched by either ... (or one) or .na will be recoded to the value passed to .default.

binary_recode() wraps recode() but accepts only a single recoding argument (one) instead of ...), sets .default to 0, and always returns an integer vector.

Value

a vector of the same length as x in which all substitutions specified in ... (or one in binary_recode()) are made. binary_recode() always returns a numeric vector while recode() returns a vector coerced according to the various recoding arguments provided.

See Also

recode, revalue, recode.

Examples

1
2
3
4
5
6
s <- c("a", "b", "c", "d", "e", "f", NA)
recode(s, `1` = c("a", "b"), .default = 0)
recode(s, `1` = c("a", "b"), .na = c(NA, "d", "e"))
recode(s, `1` = c("a", "b"))
recode(s, `1` = c("a", "b"), factor = TRUE)
binary_recode(s, one = c("a", "b"))

sboysel/boysel documentation built on May 29, 2019, 3:24 p.m.