Skip to contents

Looks up the codelist assigned to from in the var_spec, retrieves the codelist terms from spec$codelist, and creates a new column to containing decoded values. Warns (never silently drops) on unmatched coded values.

Usage

decode_var(x, spec, dataset, from, to)

Arguments

x

A data frame.

spec

A herald_spec object or a path to a spec file.

dataset

Character. The dataset name.

from

Character. The name of the column containing coded values.

to

Character. The name of the new decoded column to create.

Value

The data frame with the new decoded column appended (invisibly).

Examples

spec <- herald_spec(
  ds_spec = data.frame(dataset = "DM", label = "Demographics",
                       stringsAsFactors = FALSE),
  var_spec = data.frame(
    dataset     = "DM",
    variable    = "SEX",
    codelist_id = "SEX",
    stringsAsFactors = FALSE
  ),
  codelist = data.frame(
    codelist_id   = c("SEX", "SEX"),
    term          = c("M", "F"),
    decoded_value = c("Male", "Female"),
    stringsAsFactors = FALSE
  )
)
dm <- data.frame(SEX = c("M", "F", "M"), stringsAsFactors = FALSE)
result <- decode_var(dm, spec, "DM", from = "SEX", to = "SEXDECODE")
result$SEXDECODE  # "Male" "Female" "Male"
#> [1] "Male"   "Female" "Male"