Learn R Programming

sjmisc (version 1.0.2)

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. See examples.

Usage

to_fac(x)

Arguments

x
A (numeric or atomic) variable or a data frame with (numeric or atomic) variables.

Value

  • A factor variable, including variable and value labels, respectively a data frame with factor variables (including variable and value labels) if x was a data frame.

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)
library(sjPlot)
# 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