A modified as.factor() method that retains the variable label,
if it exists.
Arguments
- x
A factor vector
Value
A declared or labelled variable as factor.
Examples
x <- declared(
c(1:5, -1),
labels = c(Good = 1, Bad = 5, DK = -1),
na_values = -1
)
attr(x, "label") <- "Example"
as_factor(x)
#> [1] Good 2 3 4 Bad <NA>
#> attr(,"label")
#> [1] Example
#> Levels: Good 2 3 4 Bad