Learn R Programming

REDCapCAST (version 24.12.1)

named_levels: Get named vector of factor levels and values

Description

Get named vector of factor levels and values

Usage

named_levels(
  data,
  label = "labels",
  na.label = NULL,
  na.value = 99,
  sort.numeric = TRUE
)

Value

named vector

Arguments

data

factor

label

character string of attribute with named vector of factor labels

na.label

character string to refactor NA values. Default is NULL.

na.value

new value for NA strings. Ignored if na.label is NULL. Default is 99.

sort.numeric

sort factor levels if levels are numeric. Default is TRUE

Examples

Run this code
structure(c(1, 2, 3, 2, 10, 9),
  labels = c(Unknown = 9, Refused = 10),
  class = "haven_labelled"
) |>
  as_factor() |>
  named_levels()
structure(c(1, 2, 3, 2, 10, 9),
  labels = c(Unknown = 9, Refused = 10),
  class = "labelled"
) |>
  as_factor() |>
  named_levels()

Run the code above in your browser using DataLab