Description Usage Arguments Value Acknowledgment See Also Examples
Use this function inside dplyr::recode()
to recode a vector based on values
from two other vectors, where old
and new
codes are looked up. These
lookup vectors are commonly stored in a dataframe and come from a .csv or
spreadsheet file.
1 |
old, new |
Vectors of equal length giving old and new codes. |
A "spliced" list with names from old
and values from new
. The
kind of data structure that you can feed to ...
in dplyr::recode()'.
Thanks to David Kenfack for inspiring this function.
Other general functions to edit data in place: fill_na
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | library(dplyr, warn.conflicts = FALSE)
look <- tibble::tibble(
old = c("spp1", "unknown"),
new = c("spp3", "spp4")
)
sp <- c("spp1", "spp2", "spp3", "unknown", "spp3", "unknown", "spp1", "spp1")
recode(sp, lookup(look$old, look$new))
census <- tibble::tibble(sp = sp)
mutate(census, new_sp = recode(sp, lookup(look$old, look$new)))
# Overwrite
mutate(census, sp = recode(sp, lookup(look$old, look$new)))
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.