to_value(x, start.at = NULL, keep.labels = TRUE)
factor
or a data frame with factor
s. May also be
a character vector.NULL
, hence the lowest
value of the returned numeric variable corresponds to the lowest factor
level (if factor is numeric
) or to 1
(if factor levels
are not numeric).TRUE
, former factor levels will be added as
value labels. For numeric factor levels, values labels will be used,
if present. See 'Examples' and set_labels
for more details.start.at
to
start.at
+ length of factor levels, or to the corresponding
factor levels (if these were numeric). Or a data frame with numeric
variables, if x
was a data frame.
to_label
to convert a labelled vector into a factor with labelled
factor levels and to_factor
to convert a numeric variable
into a factor (and preserve labels)
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))
# numeric factor keeps values
dummy <- factor(c("3", "4", "6"))
table(to_value(dummy))
# do not drop unused factor levels
dummy <- ordered(c(rep("No", 5), rep("Maybe", 3)),
levels = c("Yes", "No", "Maybe"))
to_value(dummy)
# non-numeric factor is converted to numeric
# starting at 1
dummy <- factor(c("D", "F", "H"))
table(to_value(dummy))
# for numeric factor levels, value labels will be used, if present
dummy1 <- factor(c("3", "4", "6"))
set_labels(dummy1) <- c("first", "2nd", "3rd")
dummy1
to_value(dummy1)
# for non-numeric factor levels, these will be used.
# value labels will be ignored
dummy2 <- factor(c("D", "F", "H"))
set_labels(dummy2) <- c("first", "2nd", "3rd")
dummy2
to_value(dummy2)
Run the code above in your browser using DataLab