data(efc)
library(sjPlot)
# normal factor conversion, loses value attributes
efc$e42dep <- as.factor(efc$e42dep)
sjt.frq(efc$e42dep)
# factor conversion, which keeps value attributes
efc$e42dep <- to_factor(efc$e42dep)
sjt.frq(efc$e42dep)
data(efc)
# create parially labelled vector
x <- set_labels(efc$e42dep,
c(`1` = "independent",
`4` = "severe dependency",
`9` = "missing value"))
# only copy existing value labels
to_factor(x)
get_labels(to_factor(x), include.values = "p")
# also add labels to non-labelled values
to_factor(x, add.non.labelled = TRUE)
get_labels(to_factor(x, add.non.labelled = TRUE), include.values = "p")
# create labelled integer, with missing flag
x <- labelled(c(1, 2, 1, 3, 4, 1),
c(Male = 1, Female = 2, Refused = 3, "N/A" = 4),
c(FALSE, FALSE, TRUE, TRUE))
# to factor, with missing labels
to_factor(x, drop.na = FALSE)
# to factor, missings removed
to_factor(x, drop.na = TRUE)
Run the code above in your browser using DataLab