as_factor {haven} | R Documentation |
The base function as.factor()
is not a generic, but this variant
is. Methods are provided for factors, character vectors, labelled
vectors, and data frames. By default, when applied to a data frame,
it only affects labelled
columns.
as_factor(x, ...) ## S3 method for class 'factor' as_factor(x, ...) ## S3 method for class 'character' as_factor(x, ...) ## S3 method for class 'data.frame' as_factor(x, ..., only_labelled = TRUE) ## S3 method for class 'labelled' as_factor(x, levels = c("default", "labels", "values", "both"), ordered = FALSE, ...)
x |
Object to coerce to a factor. |
... |
Other arguments passed down to method. |
only_labelled |
Only apply to labelled columns? |
levels |
How to create the levels of the generated factor:
|
ordered |
If |
x <- labelled(sample(5, 10, replace = TRUE), c(Bad = 1, Good = 5)) # Default method uses values where available as_factor(x) # You can also extract just the labels as_factor(x, "labels") # Or just the values as_factor(x, "values") # Or combine value and label as_factor(x, "both")