Learn R Programming

sjPlot (version 1.7)

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

Value

A factor variable with the associated value labels as factor levels.

See Also

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))

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

Run the code above in your browser using DataLab