labelled_to_factor: Convert labelled numerics to factors

View source: R/labelled_to_factor.R

labelled_to_factorR Documentation

Convert labelled numerics to factors

Description

Convert labelled numerics to factors

Usage

labelled_to_factor(x)

Arguments

x

an atomic numeric with the attribute "labels". Probably from expss::val_lab

Value

a factor

Note

This is a bit of a quirky solution because the integers underlying a factor are default set to 1 through the number of unique items. This means that without saving the original value labels

Author(s)

Sven Halvorson (svenpubmail@gmail.com)

Examples

city_labels = c(
  'Urruk' = 2,
  'Babylon' = 3,
  'Larsa' = 4,
  'Mari' = 5
)

set.seed(0)
cities = sample(2:5, size = 10, replace = TRUE)

expss::val_lab(cities) = city_labels

labelled_to_factor(cities)

# NOTE the difference:
as.numeric(labelled_to_factor(cities)) == cities

svenhalvorson/SvenR documentation built on Aug. 25, 2023, 1:31 p.m.