Learn R Programming

sjmisc (version 1.0.2)

to_label: Converts variable into factor and replaces values with associated value labels

Description

This function converts (replaces) variable values (also of factors) with their associated value labels. Might be helpful for factor variables. For instance, if you have a Gender variable with 0/1 value, and associated labels are male/female, this function would convert all 0 to male and all 1 to female and returns the new variable as factor.

Usage

to_label(x)

Arguments

x
A variable of type numeric, atomic, factor or labelled

Value

  • A factor variable with the associated value labels as factor levels, or a data frame with such factor variables (if x was a data frame).

See Also

to_fac to convert a numeric variable into a factor (and retain labels) and to_value to convert a factor into a numeric variable.

Examples

Run this code
data(efc)
print(get_val_labels(efc)['c161sex'])
head(efc$c161sex)
head(to_label(efc$c161sex))

print(get_val_labels(efc)['e42dep'])
table(efc$e42dep)
table(to_label(efc$e42dep))

head(efc$e42dep)
head(to_label(efc$e42dep))

# structure of numeric values won't be changed
# by this function, it only applies to labelled vectors
# (typically categorical or factor variables)
str(efc$e17age)
str(to_label(efc$e17age))

Run the code above in your browser using DataLab