knitr::opts_chunk$set( collapse = TRUE, comment = "#>" )
library(statficlassifications)
Recoding of categorical variables are often based on keys that map one classification of a categorical variable to an another classification of this same variable. \code{key_recode} recodes an input given a such a key. The key can be in the form of a named vector or a data.frame. The inputs can be vectors, factors of data.frames.
var1 <- c("a", "b", "a", "c", "b", "b") key <- data.frame(var1 = letters[1:4], var2 = c("first letter", "second letter", "third letter", "fourth letter"), var3 = 1:4)
By default, the variable in key corresponding to the variable to be recoded is found by name:
key_recode(var1, key, to = "var2")
The corresponding variable can however, be found using the values of variables as well:
v <- var1 key_recode(v, key, to = "var2", by = "values")
data.frames can be recoded as well:
df <- data.frame(var1 = var1, y = 1:6) key_recode(df, key, to = "var2")
Without 'to'-argument, recoding happens to all variable other than 'from'-variable in the key.
key_recode(var1, key)
Keys can be named vectors as well:
key <- c("a" = "first letter", "b" = "second letter", "c" = "third letter", "d" = "fourth letter")
key_recode(v, key)
Note that with named vectors the function knows which way you are recoding:
v <- key_recode(v, key) v v <- key_recode(v, key) v
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.