translate: Recode a variable using a matched pair of vectors.

View source: R/miscellaneous.R

translateR Documentation

Recode a variable using a matched pair of vectors.

Description

Recode a variable using a pair of vectors matched by row (i.e. a dictionary of the sort commonly made using a spreadsheet). This allows you to translate a variable from one coding scheme to another using arbitrary conditional matching in situations where it would be overly tedious to use recode, if_else, or case_when to recode a variable because there are many conditional replacements to be specified.

Usage

translate(y, old, new)

Arguments

y

A vector that you wish to translate/recode.

old

A vector containing values of the old coding scheme with corresponding values/rows in "new". Should be the same length as the "new" vector.

new

A vector containing values of the new coding scheme with corresponding values/rows in "old". Should be the same length as the "old" vector.

Value

An updated version of the input vector that has been translated from the old coding scheme to the new one.

Author(s)

Craig P. Hutton, Craig.Hutton@gov.bc.ca

See Also

match

Examples


data(mtcars)

old_values <- c(1:10)

new_values <- c("one", "two", "three", "four", "five",
                "six", "seven", "eight", "nine", "ten")

#use it on its own
translate(y = mtcars$cyl, old = old_values, new = new_values)

#or within a dplyr::mutate call
dplyr::mutate(mtcars,
              translated_cyl = translate(cyl,
                                         old = old_values,
                                         new = new_values))


bcgov/elucidate documentation built on Sept. 3, 2022, 7:16 p.m.