Learn R Programming

sjmisc (version 1.0.0)

to_value: Converts factors to numeric variables

Description

This function converts (replaces) factor values with the related factor level index number, thus the factor is converted to a numeric variable.

Usage

to_value(x, startAt = 1, keep.labels = TRUE)

Arguments

x
A (factor) variable.
startAt
the starting index, i.e. the lowest numeric value of the variable's value range.
keep.labels
logical, if TRUE, former factor levels will be attached as value labels. See set_val_labels for more details.

Value

  • A numeric variable with values ranging from startAt to startAt + length of factor levels.

See Also

to_label to convert a value into a factor with labelled factor levels and to_fac to convert a numeric variable into a factor (and retain labels)

Examples

Run this code
data(efc)
test <- to_label(efc$e42dep)
table(test)

table(to_value(test))
hist(to_value(test, 0))

# set lowest value of new variable
# to "5".
table(to_value(test, 5))

Run the code above in your browser using DataLab