# NOT RUN {
if (require("sjmisc") && require("magrittr")) {
data(efc)
# normal factor conversion, loses value attributes
x <- as.factor(efc$e42dep)
frq(x)
# factor conversion, which keeps value attributes
x <- as_factor(efc$e42dep)
frq(x)
# create partially labelled vector
x <- set_labels(
efc$e42dep,
labels = c(
`1` = "independent",
`4` = "severe dependency",
`9` = "missing value"
))
# only copy existing value labels
as_factor(x) %>% head()
get_labels(as_factor(x), values = "p")
# also add labels to non-labelled values
as_factor(x, add.non.labelled = TRUE) %>% head()
get_labels(as_factor(x, add.non.labelled = TRUE), values = "p")
# easily coerce specific variables in a data frame to factor
# and keep other variables, with their class preserved
as_factor(efc, e42dep, e16sex, c172code) %>% head()
# use select-helpers from dplyr-package
if (require("dplyr")) {
as_factor(efc, contains("cop"), c161sex:c175empl) %>% head()
}
}
# }
Run the code above in your browser using DataLab