to_numeric {datawizard} | R Documentation |
Convert data to numeric
Description
Convert data to numeric by converting characters to factors and factors to
either numeric levels or dummy variables. The "counterpart" to convert
variables into factors is to_factor()
.
Usage
to_numeric(x, ...)
## S3 method for class 'data.frame'
to_numeric(
x,
select = NULL,
exclude = NULL,
dummy_factors = TRUE,
preserve_levels = FALSE,
lowest = NULL,
append = FALSE,
ignore_case = FALSE,
regex = FALSE,
verbose = TRUE,
...
)
Arguments
x |
A data frame, factor or vector. |
... |
Arguments passed to or from other methods. |
select |
Variables that will be included when performing the required tasks. Can be either
If |
exclude |
See |
dummy_factors |
Transform factors to dummy factors (all factor levels as different columns filled with a binary 0-1 value). |
preserve_levels |
Logical, only applies if |
lowest |
Numeric, indicating the lowest (minimum) value when converting factors or character vectors to numeric values. |
append |
Logical or string. If |
ignore_case |
Logical, if |
regex |
Logical, if |
verbose |
Toggle warnings. |
Value
A data frame of numeric variables.
Selection of variables - select
argument
For most functions that have a select
argument the complete input data
frame is returned, even when select
only selects a range of variables.
However, for to_numeric()
, factors might be converted into dummies,
thus, the number of variables of the returned data frame no longer match
the input data frame. Hence, when select
is used, only those variables
(or their dummies) specified in select
will be returned. Use append=TRUE
to also include the original variables in the returned data frame.
Note
By default, to_numeric()
converts factors into "binary" dummies, i.e.
each factor level is converted into a separate column filled with a binary
0-1 value. If only one column is required, use dummy_factors = FALSE
. If
you want to preserve the original factor levels (in case these represent
numeric values), use preserve_levels = TRUE
.
Examples
to_numeric(head(ToothGrowth))
to_numeric(head(ToothGrowth), dummy_factors = FALSE)
# factors
x <- as.factor(mtcars$gear)
to_numeric(x, dummy_factors = FALSE)
to_numeric(x, dummy_factors = FALSE, preserve_levels = TRUE)
# same as:
coerce_to_numeric(x)