recode: Recode variables based on a pre-defined mapping table.

Description Usage Arguments Value Examples

View source: R/recode.R

Description

Recode variables based on a pre-defined mapping table.

Usage

1
recode(x, table, verbose = TRUE)

Arguments

x

a data.frame

table

a data.frame with three columns: variable, category, new_category

Value

a recoded data.frame.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
# the main data frame to be recoded
x <- data.frame(
  id = 1:10,
  gender = sample(c("m", "f", "fe"), 10, replace = TRUE),
  labour = sample(c("employed, fulltime", "employed, part-time", "unemployed"), 10, replace = TRUE)
)

# create a mapping table
my_table <- data.frame(variable = c(rep("gender",3), rep("labour", 3)),
                       category = c("m", "f", "fe",
                                    "employed, fulltime", "employed, part-time", "unemployed"),
                       new_category = c("male", "female", "female",
                                        "employed", "employed", "unemployed"))

x_new <- recodeR::recode(x = x, table = my_table, verbose = TRUE)

asiripanich/recodeR documentation built on Nov. 3, 2019, 1:58 p.m.