to_factor {datawizard} | R Documentation |
Convert data to factors
Description
Convert data to factors
Usage
to_factor(x, ...)
## S3 method for class 'numeric'
to_factor(x, labels_to_levels = TRUE, verbose = TRUE, ...)
## S3 method for class 'data.frame'
to_factor(
x,
select = NULL,
exclude = NULL,
ignore_case = FALSE,
append = FALSE,
regex = FALSE,
verbose = TRUE,
...
)
Arguments
x |
A data frame or vector. |
... |
Arguments passed to or from other methods. |
labels_to_levels |
Logical, if |
verbose |
Toggle warnings. |
select |
Variables that will be included when performing the required tasks. Can be either
If |
exclude |
See |
ignore_case |
Logical, if |
append |
Logical or string. If |
regex |
Logical, if |
Details
Convert variables or data into factors. If the data is labelled, value labels
will be used as factor levels. The counterpart to convert variables into
numeric is to_numeric()
.
Value
A factor, or a data frame of factors.
Selection of variables - the select
argument
For most functions that have a select
argument (including this function),
the complete input data frame is returned, even when select
only selects
a range of variables. That is, the function is only applied to those variables
that have a match in select
, while all other variables remain unchanged.
In other words: for this function, select
will not omit any non-included
variables, so that the returned data frame will include all variables
from the input data frame.
Note
Factors are ignored and returned as is. If you want to use value labels
as levels for factors, use labels_to_levels()
instead.
Examples
str(to_factor(iris))
# use labels as levels
data(efc)
str(efc$c172code)
head(to_factor(efc$c172code))