as_labelled: Convert a factor to a haven::labelled variable

View source: R/utils.R

as_labelledR Documentation

Convert a factor to a haven::labelled variable

Description

Convert a factor to a haven::labelled variable

Usage

as_labelled(x)

Arguments

x

Factor.

Value

Returns a labelled variable with values and labels according to the levels in the factor. This should roundtrip with forcats::as_factor. Additionally, a variable label can be set.

Examples

x <- factor(c("b", "b", "a"))
attr(x, "label") <- "variable label"
x
as_labelled(x)
all.equal(x,
          x %>%
            as_labelled() %>%
            forcats::as_factor())

urswilke/tablab documentation built on Oct. 17, 2022, 8:19 p.m.