Description Usage Arguments Details Value Examples
Replaces numeric or character values in vector
with matching entries from a named
vector or list provided in definitions
.
1 2 3 | def_recode(.x, definitions, .default = NULL, .missing = NULL)
def_recode_pick(.x, def_name, def_list, .default = NULL, .missing = NULL)
|
.x |
A vector to modify |
definitions |
A named vector or named list of replacement values, probably one entry in a dictionary
produced by |
.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 |
A list of definition sets, likely produced by |
* 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.
Vector of same length and order as .x
, but with values drawn from definitions
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)
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.