labels: Convert vector with codes to factor using a code list

labels.codeR Documentation

Convert vector with codes to factor using a code list

Description

Convert vector with codes to factor using a code list

Usage

## S3 method for class 'code'
labels(
  object,
  missing = TRUE,
  droplevels = FALSE,
  codelist = attr(object, "codelist"),
  locale = cl_locale(codelist),
  ...
)

to_labels(
  x,
  codelist = attr(x, "codelist"),
  missing = TRUE,
  droplevels = FALSE,
  locale = cl_locale(codelist)
)

Arguments

object

vector with codes. Should be of the same type as the codes in the codelist.

missing

convert codes that are missing value to missing values.

droplevels

remove labels that do not occur in x.

codelist

a codelist object or a data.frame that is a valid code list.

locale

use the codes from the given locale. Should be character vector of length 1.

...

ignored

x

vector with codes. Should be of the same type as the codes in the codelist.

Details

to_labels calls labels.code directly and is meant as a substitute for labels.code for objects that are not of type 'code'.

Value

A factor vector with the same length as x.

Examples

data(objectsales)
data(objectcodes)
objectsales$product <- code(objectsales$product, objectcodes)

labels(objectsales$product) |> 
  table(useNA = "ifany")
labels(objectsales$product, missing = FALSE) |> 
  table(useNA = "ifany")
labels(objectsales$product, droplevels = TRUE) |> 
  table(useNA = "ifany")

to_labels(c("A", "B"), codelist = objectcodes)
# is the same as 
labels.code(c("A", "B"), codelist = objectcodes)


codelist documentation built on April 12, 2025, 2:25 a.m.