Check that a vector is labelled in a given way.

chk_labels(x, val_labels = NULL, var_label = NULL)

Arguments

x

A vector to check.

val_labels

What value label check should be performed? One of:

  • A character vector of expected value labels.

  • A named vector of expected label-value pairs.

  • TRUE to test for the presence of value labels in general.

  • FALSE to test for the absence of value labels.

  • NULL to ignore value labels when checking.

var_label

What variable label check should be performed? One of:

  • A character vector of expected variable labels.

  • TRUE to test for the presence of a variable labels.

  • FALSE to test for the absence of a variable labels.

  • NULL to ignore the variable label when checking.

Value

A logical vector flagging records that have passed or failed the check.

Examples


df <- data.frame(
  x = labelled::labelled(c("M", "M", "F"), c(Male = "M", Female = "F"), "Sex"),
  y = labelled::labelled(c("M", "M", "F"), c(Male = "M", Female = "F", Other = "X")),
  z = c("M", "M", "F")
)

# Check for a value-label pairing
chk_labels(df$x, c(Male = "M"))
#> [1]  TRUE  TRUE FALSE

# Check that two variables have the same values
chk_labels(df$x, labelled::val_labels(df$y))
#> [1] TRUE TRUE TRUE

# Check for the presence of a particular label
chk_labels(df$x, "Male")
#> [1]  TRUE  TRUE FALSE
chk_labels(df$x, var_label = "Sex")
#> [1] TRUE TRUE TRUE

# Check that a variable is labelled at all
chk_labels(df$z, val_labels = TRUE)
#> [1] FALSE FALSE FALSE
chk_labels(df$z, var_label = TRUE)
#> [1] FALSE FALSE FALSE

# Check that a variable isn't labelled
chk_labels(df$z, val_labels = FALSE)
#> [1] TRUE TRUE TRUE
chk_labels(df$z, var_label = FALSE)
#> [1] TRUE TRUE TRUE