The base function base::as.factor() is not a generic, but this variant
is. By default, to_factor is a wrapper for base::as.factor().
Please note that to_factor differs slightly from as_factor
method provided by haven package.
to_factor(x, ...) # S3 method for haven_labelled to_factor(x, levels = c("labels", "values", "prefixed"), ordered = FALSE, nolabel_to_na = FALSE, sort_levels = c("auto", "none", "labels", "values"), decreasing = FALSE, drop_unused_labels = FALSE, user_na_to_na = FALSE, strict = FALSE, unclass = FALSE, ...) # S3 method for data.frame to_factor(x, levels = c("labels", "values", "prefixed"), ordered = FALSE, nolabel_to_na = FALSE, sort_levels = c("auto", "none", "labels", "values"), decreasing = FALSE, labelled_only = TRUE, drop_unused_labels = FALSE, strict = FALSE, unclass = FALSE, ...)
| x | Object to coerce to a factor. |
|---|---|
| ... | Other arguments passed down to method. |
| levels | What should be used for the factor levels: the labels, the values or labels prefixed with values? |
| ordered |
|
| nolabel_to_na | Should values with no label be converted to `NA`? |
| sort_levels | How the factor levels should be sorted? (see Details) |
| decreasing | Should levels be sorted in decreasing order? |
| drop_unused_labels | Should unused value labels be dropped?
(applied only if |
| user_na_to_na | Convert user defined missing values into |
| strict | Convert to factor only if all values have a defined label? |
| unclass | If not converted to a factor (when |
| labelled_only | for a data.frame, convert only labelled variables to factors? |
If some values doesn't have a label, automatic labels will be created, except if
nolabel_to_na is TRUE.
If sort_levels == 'values', the levels will be sorted according to the values of x.
If sort_levels == 'labels', the levels will be sorted according to labels' names.
If sort_levels == 'none', the levels will be in the order the value labels are defined
in x. If some labels are automatically created, they will be added at the end.
If sort_levels == 'auto', sort_levels == 'none' will be used, except if some
values doesn't have a defined label. In such case, sort_levels == 'values' will
be applied.
When applied to a data.frame, only labelled vectors are converted by default to a
factor. Use labelled_only = FALSE to convert all variables to factors.
#> [1] yes 2 2 2 no don't know #> [7] yes no 2 <NA> #> Levels: yes 2 no don't knowto_factor(v, nolabel_to_na = TRUE)#> [1] yes <NA> <NA> <NA> no don't know #> [7] yes no <NA> <NA> #> Levels: yes no don't knowto_factor(v, 'p')#> [1] [1] yes [2] 2 [2] 2 [2] 2 [3] no #> [6] [9] don't know [1] yes [3] no [2] 2 <NA> #> Levels: [1] yes [2] 2 [3] no [9] don't knowto_factor(v, sort_levels = 'v')#> [1] yes 2 2 2 no don't know #> [7] yes no 2 <NA> #> Levels: yes 2 no don't knowto_factor(v, sort_levels = 'n')#> [1] yes 2 2 2 no don't know #> [7] yes no 2 <NA> #> Levels: yes no don't know 2to_factor(v, sort_levels = 'l')#> [1] yes 2 2 2 no don't know #> [7] yes no 2 <NA> #> Levels: 2 don't know no yesx <- labelled(c('H', 'M', 'H', 'L'), c(low = 'L', medium = 'M', high = 'H')) to_factor(x, ordered = TRUE)#> [1] high medium high low #> Levels: low < medium < high#> [1] No No Yes 3 #> Levels: No Yes 3to_factor(v, strict = TRUE) # Not converted because 3 does not have a label#> <Labelled double> #> [1] 1 1 2 3 #> #> Labels: #> value label #> 1 No #> 2 Yesto_factor(v, strict = TRUE, unclass = TRUE)#> [1] 1 1 2 3