Learn R Programming

DCC (version 1.2.1)

dcc_apply_codebook: Apply a declarative codebook to a dataset

Description

Applies a codebook to a dataset: per variable it can rename, recode values, declare missing codes, coerce type, and attach a label, value labels, and a role. The preview (dry_run = TRUE) describes every change and changes nothing; dry_run = FALSE returns a new dcc_data with a codebook provenance record. The raw input is never overwritten, and the preview and apply share one planner, so a change is previewed exactly as applied. Unknown variables and impossible type coercions raise dcc_codebook_error.

Usage

dcc_apply_codebook(x, codebook, dry_run = TRUE)

Value

A dcc_codebook_preview (dry run) or a new dcc_data.

Arguments

x

A dcc_data object or data.frame.

codebook

A named list keyed by (current) variable name. Each entry is a list with any of: rename (new name), recode (a named old -> new value map), missing (values set to NA), type (target class), label, value_labels (a named vector), and role.

dry_run

If TRUE (default), return a dcc_codebook_preview; if FALSE, apply and return a new dcc_data.

See Also

dcc_codebook_changes.

Examples

Run this code
df <- data.frame(sid = c("S1", "S2"), age = c(25, -99),
                 sex = c("1", "2"), stringsAsFactors = FALSE)
cb <- list(
  age = list(missing = -99, type = "integer"),
  sex = list(rename = "gender", recode = c("1" = "M", "2" = "F"),
             label = "Gender")
)
dcc_apply_codebook(df, cb)
dcc_apply_codebook(df, cb, dry_run = FALSE)

Run the code above in your browser using DataLab