Learn R Programming

rcoder (version 0.3.0)

recode_vec: Recode a vector

Description

A simple interface to recoding a vector based on the coding linking mechanism. If the vector has the "rcoder.coding" attribute, then the coding object stored in that attribute will be used by default.

Usage

recode_vec(vec, to, from = NULL, .embed = TRUE, .bpr = TRUE)

Value

The recoded vector

Arguments

vec

A vector

to

A coding object to which the vector will be recoded

from

A coding object that describes the current coding of the vector. Defaults to the "rcoder.coding" attribute value, if it exists, _or_ the "bpr.coding" value (from blueprintr). If neither are found, `from` stays `NULL` and the function errors.

.embed

If `TRUE`, `from` will be stored in the "rcoder.coding" attribute

.bpr

If `TRUE`, adds the _character_ representation of the coding to the "bpr.coding" attribute. Used for interop with blueprintr variable decorations

See Also

[assign_coding()]

Examples

Run this code
# Using an explicit `from`
vec <- sample(1:3, 50, replace = TRUE)
cdng_old <- coding(code("Yes", 3), code("Maybe", 2), code("No", 1))
cdng_new <- coding(code("Yes", 2), code("Maybe", 1), code("No", 0))
recode_vec(vec, to = cdng_new, from = cdng_old)

# Using an implicit `from` with assign_coding()
vec <- sample(1:3, 50, replace = TRUE)
vec <- assign_coding(vec, cdng_old)
recode_vec(vec, cdng_new)

Run the code above in your browser using DataLab