Description Usage Arguments Examples
Converts values of a data frame to encoded values using a dictionary.
1 | apply_code(df, dict, oldVar, newVar)
|
df |
data frame to change |
dict |
Assumes that the dictionary will be a 2-column data frame with key in first column and value in the second. Third column is an optional column explaining the relationship. Should be some sort of logical operator, e.g. '==', '<', '<=', ... |
oldVar |
name of the variable to change |
newVar |
name of the new variable to generate |
1 2 3 4 5 6 7 8 9 10 11 | df = data.frame(region_code = sample(6, 10, replace = TRUE))
dict = data.frame(region_code = 1:6, region_name = c('Africa', 'Asia', 'Australia', 'Europe', 'North America', 'South America'))
# straight dictionary
llamar::apply_code(df, dict, 'region_code', 'name')
# fancy math
dict2 = data.frame(region_code = 1:5,
region_name = c('Africa', 'Asia', 'Australia', 'Europe', 'Americas'),
logic = c(rep('==', 4), '>=')) # If >= 5, assign to Americas.
llamar::apply_code(df, dict2, 'region_code', 'name')
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.