to_numeric | R Documentation |
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()
.
to_numeric(x, ...)
## S3 method for class 'data.frame'
to_numeric(
x,
select = NULL,
exclude = NULL,
dummy_factors = FALSE,
preserve_levels = FALSE,
lowest = NULL,
append = FALSE,
ignore_case = FALSE,
regex = FALSE,
verbose = TRUE,
...
)
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. |
A data frame of numeric variables.
select
argumentFor 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.
When factors should be converted into multiple "binary" dummies, i.e.
each factor level is converted into a separate column filled with a binary
0-1 value, set dummy_factors = TRUE
. If you want to preserve the original
factor levels (in case these represent numeric values), use
preserve_levels = TRUE
.
to_numeric(head(ToothGrowth))
to_numeric(head(ToothGrowth), dummy_factors = TRUE)
# factors
x <- as.factor(mtcars$gear)
to_numeric(x)
to_numeric(x, preserve_levels = TRUE)
# same as:
coerce_to_numeric(x)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.