replace_df {COINr} | R Documentation |
Replace multiple values in a data frame
Description
Given a data frame (or vector), this function replaces values according to a look up table or dictionary. In COINr this may be useful for exchanging categorical data with numeric scores, prior to assembly. Or for changing codes.
Usage
replace_df(df, lookup)
Arguments
df |
A data frame or a vector |
lookup |
A data frame with columns |
Details
The lookup data frame must not have any duplicated values in the old
column. This function looks for exact matches of
elements of the old
column and replaces them with the corresponding value in the new
column. For each row of lookup
,
the class of the old value must match the class of the new value. This is to keep classes of data frames columns consistent.
If you wish to replace with a different class, you should convert classes in your data frame before using this function.
This function replaces the now-defunct replaceDF()
from COINr < v1.0.
Value
A data frame with replaced values
Examples
# replace sub-pillar codes in ASEM indicator metadata
codeswap <- data.frame(old = c("Conn", "Sust"), new = c("SI1", "SI2"))
# swap codes in both iMeta
replace_df(ASEM_iMeta, codeswap)