lookup: Modify a vector to replace old with new values from a lookup...

Description Usage Arguments Value Acknowledgment See Also Examples

Description

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.

Usage

1
lookup(old, new)

Arguments

old, new

Vectors of equal length giving old and new codes.

Value

A "spliced" list with names from old and values from new. The kind of data structure that you can feed to ... in dplyr::recode()'.

Acknowledgment

Thanks to David Kenfack for inspiring this function.

See Also

dplyr::recode()

Other general functions to edit data in place: fill_na

Examples

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

forestgeo/fgeo.misc documentation built on June 23, 2019, 6:26 p.m.