key_recode: Recode with key

View source: R/key_recode.R

key_recodeR Documentation

Recode with key

Description

Given a key, recodes input.

Usage

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

Arguments

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

"names", "values" or "all_values".

  • "names" uses column names to join key to input.

  • "values" looks for the most suitable from-column in the key by comparing the values in columns. The from-column is chosen as the one that has most of the values of the input vector.

  • "all_values" requires that all values are in the from-column.

add

whether to add (or replace) the original vector to be recoded. Defaults to FALSE.

Details

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.

Value

vector or data.frame

Examples


  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")



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