applyLookup {eatGADS} | R Documentation |
Recode via lookup table.
Description
Recode one or multiple variables based on a lookup table created via createLookup
(and potentially formatted by collapseColumns
).
Usage
applyLookup(GADSdat, lookup, suffix = NULL)
Arguments
GADSdat |
A |
lookup |
Lookup table created by |
suffix |
Suffix to add to the existing variable names. If |
Details
If there are missing values in the column value_new
, NAs
are inserted as new values
and a warning
is issued.
The complete work flow when using a lookup table to recode multiple variables in a GADSdat
could be:
(0) optional: Recode empty strings to NA
(necessary, if the look up table is written to excel).
(1) create a lookup table with createLookup
.
(2) Save the lookup table to .xlsx
with write_xlsx
from eatAnalysis
.
(3) fill out the lookup table via Excel
.
(4) Import the lookup table back to R
via read_excel
from readxl
.
(5) Apply the final lookup table with applyLookup
.
See applyLookup_expandVar
for recoding a single variable into multiple variables.
Value
Returns a recoded GADSdat
.
Examples
## create an example GADSdat
iris2 <- iris
iris2$Species <- as.character(iris2$Species)
gads <- import_DF(iris2)
## create Lookup
lu <- createLookup(gads, recodeVars = "Species")
lu$value_new <- c("plant 1", "plant 2", "plant 3")
## apply lookup table
gads2 <- applyLookup(gads, lookup = lu, suffix = "_r")
## only recode some values
lu2 <- createLookup(gads, recodeVars = "Species")
lu2$value_new <- c("plant 1", "plant 2", NA)
gads3 <- applyLookup(gads, lookup = lu2, suffix = "_r")