Learn R Programming

sjmisc (version 1.0.0)

to_fac: Convert variable into factor and keep value labels

Description

This function converts a variable into a factor, but keeps variable and value labels, if these are attached as attributes to the variale var. See examples.

Usage

to_fac(x)

Arguments

x
A (numeric or atomic) variable.

Value

  • A factor variable, including variable and value labels.

See Also

to_value to convert a factor into a numeric value and to_label to convert a value into a factor with labelled factor levels.

Examples

Run this code
data(efc)
# normal factor conversion, loses value attributes
efc$e42dep <- as.factor(efc$e42dep)
sjt.frq(efc$e42dep)

data(efc)
# factor conversion, which keeps value attributes
efc$e42dep <- to_fac(efc$e42dep)
sjt.frq(efc$e42dep)

Run the code above in your browser using DataLab