View source: R/miscellaneous.R
| translate | R Documentation |
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.
translate(y, old, new)
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. |
An updated version of the input vector that has been translated from the old coding scheme to the new one.
Craig P. Hutton, Craig.Hutton@gov.bc.ca
match
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))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.