harmonize_values: Harmonize values and labels of labelled vectors

View source: R/harmonize_values.R

harmonize_valuesR Documentation

Harmonize values and labels of labelled vectors

Description

Convert heterogeneous labelled survey vectors into a harmonized representation with standardized value labels, numeric codes, and missing value definitions.

Usage

harmonize_values(
  x,
  harmonize_label = NULL,
  harmonize_labels = NULL,
  na_values = c(do_not_know = 99997, declined = 99998, inap = 99999),
  na_range = NULL,
  id = "survey_id",
  name_orig = NULL,
  remove = NULL,
  perl = FALSE
)

Arguments

x

A labelled survey vector.

harmonize_label

Optional harmonized variable label. Defaults to the original variable label.

harmonize_labels

A list or data frame containing from, to, and numeric_values.

na_values

Named numeric vector defining harmonized user-defined missing values.

na_range

Reserved for compatibility with the original interface. The source vector's missing-value range is retained.

id

Survey identifier. If NULL, inherit the identifier from x, when available.

name_orig

Reserved for compatibility with the original interface. The original object name is derived from x.

remove

Optional regular expression identifying characters to remove before matching.

perl

Logical. Use Perl-compatible regular expressions during preliminary label matching.

Details

The harmonization rules supplied through harmonize_labels must contain three elements:

  • from: regular expressions matching original value labels.

  • to: harmonized value labels.

  • numeric_values: harmonized numeric codes.

Matching is case-insensitive. Periods and parentheses are removed before matching.

For exact value-label mappings defined in a crosswalk table, use crosswalk_surveys().

Value

A retroharmonize_labelled_spss_survey vector containing harmonized values, labels, missing value definitions, and source-specific coding metadata.

See Also

crosswalk_surveys(), harmonize_var_names()

Other harmonization functions: collect_val_labels(), create_codebook(), crosswalk_surveys(), harmonize_na_values(), harmonize_survey_values(), harmonize_var_names(), is.crosswalk_table(), label_normalize()

Examples

var1 <- labelled::labelled_spss(
  x = c(1, 0, 1, 8, 9),
  labels = c(
    "TRUST" = 1,
    "NOT TRUST" = 0,
    "DON'T KNOW" = 8,
    "INAP. HERE" = 9
  ),
  na_values = c(8, 9)
)

harmonize_values(
  var1,
  harmonize_labels = list(
    from = c("^trust", "^not", "^don", "^inap"),
    to = c("trust", "not_trust", "do_not_know", "inap"),
    numeric_values = c(1, 0, 99997, 99999)
  ),
  na_values = c("do_not_know" = 99997, "inap" = 99999)
)


retroharmonize documentation built on Nov. 29, 2026, 5:06 p.m.