Description Usage Arguments Details Value Author(s) Examples
This function converts all columns of a data frame to class numeric
for which this conversion is possible
i.e. without creating NA
when it fails. Non-convertible columns are maintained. Optionally,
only a logical vector indicating which columns are convertible is returned.
1 2 |
dataFrame |
A data frame which should be converted. |
set.numeric |
Logical: If |
transform.factors |
Logical indicating whether columns of class |
maintain.factor.scores |
Logical:
If |
verbose |
Logical: If |
ignoreAttributes |
Logical: If |
In R, factors may represent ordered categories or categorical variables. Depending on the meaning of the variable, a conversion
of the nominal values (of a factor variable) to numeric values may be desirable or not. The arguments transform.factors
and maintain.factor.scores
specify if and how factor variables should be treated. See examples.
Either a logical vector indicating which columns in the data frame are convertible to class numeric
according to the specified conditions or a data frame in which all convertible columns are converted to class numeric
.
Sebastian Weirich
1 2 3 4 5 6 7 8 9 | dat <- data.frame(X1 = c("1",NA,"0"), X2 = c("a",NA,"b"),
X3 = c(TRUE,FALSE,FALSE), X4 = as.factor(c("a",NA,"b")),
X5 = as.factor(c("5","6","7")), stringsAsFactors = FALSE)
str(dat)
asNumericIfPossible(dat)
asNumericIfPossible(dat, transform.factors=TRUE,
maintain.factor.scores=FALSE)
asNumericIfPossible(dat, transform.factors=TRUE,
maintain.factor.scores=TRUE)
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.