apply_code: Recoding and cleaning of survey data

Description Usage Arguments Examples

Description

Converts values of a data frame to encoded values using a dictionary.

Usage

1
apply_code(df, dict, oldVar, newVar)

Arguments

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

Examples

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

flaneuse/svywrangler documentation built on May 24, 2019, 2:49 p.m.