key_recode | R Documentation |
Given a key, recodes input.
key_recode(
x,
key,
from = NULL,
to = NULL,
x_name = deparse(substitute(x)),
by = "names",
add = FALSE
)
x |
vector of elements to be recoded. |
key |
the key to use in recoding. |
from |
source column in key for recoding. |
to |
target column in key for recoding. |
x_name |
name of the vector (variable) to be recoded. |
by |
|
add |
whether to add (or replace) the original vector to be recoded.
Defaults to |
In most of the cases is able to recode an input given just a key. That is, given the information in input and in key is able to 1) determine which column in key corresponds to the input and thus from which column in key recoding is from and 2) in case of a data.frame input, which column in the data.frame is in the key and so which column in the data.frame can be recoded with the key. In these cases, the required arguments are just the object to be recoded and a key.
Keys can be data.frames, named vectors or lists of named vectors. Inputs can be vectors, factors or data.frames.
First uses prepare_key()
to transform the key into a list object that
contains, in addition to the key, data on which column of the key, the
from-column, is supposed to be recoded in to which column of the key, the
to-column.
In creating the new list object, prepare_key()
uses match_col()
to find
the column in key that has the same classification as the input vector and
potentially the column in input that can be recoded with the key.
vector or data.frame
v <- c("a", "b", "a", "c", "b", "b")
key_data.frame <- data.frame(var1 = letters[1:4],
var2 = c("first letter",
"second letter",
"third letter",
"fourth letter"))
key_recode(v, key_data.frame, by = "values")
key_recode(key_recode(v, key_data.frame, by = "values"), key_data.frame, by = "values")
key_named_vector <- c("a" = "first letter",
"b" = "second letter",
"c" = "third letter",
"d" = "fourth letter")
key_recode(v, key_named_vector)
key_recode(key_recode(v, key_named_vector), key_named_vector)
f <- factor(c("a", "b", "a", "c", "b", "b"))
key_data.frame <- data.frame(var1 = letters[1:4],
var2 = c("first letter",
"second letter",
"third letter",
"fourth letter"))
key_recode(f, key_data.frame, by = "values")
key_recode(key_recode(f, key_data.frame, by = "values"), key_data.frame, by = "values")
key_named_vector <- c("a" = "first letter",
"b" = "second letter",
"c" = "third letter",
"d" = "fourth letter")
key_recode(f, key_named_vector, by = "values")
key_recode(key_recode(f, key_named_vector), key_named_vector)
df <- data.frame(var1 = v,
y = rnorm(6))
key_recode(df, key_data.frame, by = "values")
key_recode(df, key_data.frame, from = "var1", to = "var2")
key_recode(key_recode(df, key_data.frame), key_data.frame)
key_recode(df, key_named_vector)
key_recode(key_recode(df, key_data.frame), key_named_vector)
x <- data.frame(a = c("a1", "a2"), b = c("b1", "b2"))
key <- list(a = c("a1" = "first", "a2" = "second"),
b = c("b1" = "other", "b2" = "something"))
key_recode(x, key)
key_recode(key_recode(x, key), key)
key$c <- key$a
key$a <- NULL
key_recode(x, key, by = "values")
key_recode(x, key, by = "names")
# Consider iris data
data("iris")
key <- data.frame(Species = c("setosa", "versicolor", "virginica"),
common_name = c("bristle-pointed iris",
"harlequin blueflag", "Virginia blueflag"))
key_recode(iris, key, by = "names")
# Things are often not so happily that the column names in data and the
# key would match.
names(key)[1] <- "Genus"
key_recode(iris, key, by = "values")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.