prepare_key: Prepare key for recoding with key

View source: R/key_recode.R

prepare_keyR Documentation

Prepare key for recoding with key

Description

If argument from and to are given, selects these columns from the key. If these arguments are not given, chooses the most suitable from-column from the key given either by the name of x-argument or by the values of x-argument.

Usage

prepare_key(
  x = NULL,
  key,
  from = NULL,
  to = NULL,
  x_name = deparse(substitute(x)),
  by = "names",
  add = FALSE
)

Arguments

x

vector

key

named vector, data.frame or a list

from

source column in ky

to

target column in key

x_name

name of the source column to be decoded

by

"names", "values" or "all_values". Controls how x is matched to from column in key

Details

key can be given either as a named vector, data.frame or a list. List is i interpreted as a list of keys and if a list is given, a suitable key from the list is found either by the name of x-argument or by the values of x-argument.

Also ensures that the prepared key does not have duplicate rows. Duplicate rows may occur when keys with multiple levels (e.g. regional levels) are are used such that the from-column is some higher level.

Value

list containing the prepared key, from-column name and to-column name(s).

Examples


key <- data.frame(var1 = letters[1:4],
                  var2 = c("first letter",
                           "second letter",
                           "third letter",
                           "fourth letter"),
                  var3 = 1:4)

prepare_key(key = key, from = "var1", to = "var3")


   # Consider iris data
   data("iris")

   key <- data.frame(Species = c("setosa", "versicolor", "virginica"),
                     common_name = c("bristle-pointed iris",
                      "harlequin blueflag", "Virginia blueflag"))
   prepare_key(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"
   prepare_key(iris, key, by = "values")


pttry/statficlassifications documentation built on Jan. 17, 2024, 4:36 p.m.