def_recode: Recode a single vector or return vector unchanged

Description Usage Arguments Details Value Examples

View source: R/run-recode.R

Description

Replaces numeric or character values in vector with matching entries from a named vector or list provided in definitions.

Usage

1
2
3
def_recode(.x, definitions, .default = NULL, .missing = NULL)

def_recode_pick(.x, def_name, def_list, .default = NULL, .missing = NULL)

Arguments

.x

A vector to modify

definitions

A named vector or named list of replacement values, probably one entry in a dictionary produced by def_prep(). If this explicitly passed null, .x will be returned as is

.default

If supplied, all values not otherwise matched will be given this value. If not supplied and if the replacements are the same type as the original values in .x, unmatched values are not changed. If not supplied and if the replacements are not compatible, unmatched values are replaced with NA.

.default must be either length 1 or the same length as .x.

.missing

If supplied, any missing values in .x will be replaced by this value. Must be either length 1 or the same length as .x.

def_name

Scalar character matching a name in def_list. Which set of definitions should be pulled out of def_list?

def_list

A list of definition sets, likely produced by def_prep().

Details

* def_recode() Recodes a vector based on a set of value definitions provided directly. * def_recode_pick() Recodes a vector based on a set of value definitions chosen from a list of definition sets.

This function is essentially equivalent to running dplyr::recode(.x, !!!definitions) but returns the original values if it is called with an empty definitions set.

Value

Vector of same length and order as .x, but with values drawn from definitions

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
definitions <- tibble::tibble(
  variable   = c("A", "A", "A"),
  value      = c(1,   2,   3),
  definition = c("cat", "dog", "fish")
) %>%
  def_prep(variable, value, definition)

def_recode(1:3, definitions$A)
# is equivalent to
def_recode_pick(1:3, "A", definitions)

# or, using dplyr code directly:
dplyr::recode(1:3, !!!definitions$A)
# or
dplyr::recode(1:3, "cat", "dog", "fish")

# Note that using an empty / NULL definition set returns the original vector unchanged
def_recode(1:3, definitions$Z)
def_recode_pick(1:3, "Z", definitions)

davisadamw/definer documentation built on March 11, 2020, 4:14 a.m.