Description Usage Arguments Details Value Author(s) Examples
View source: R/asNumericIfPossible.R
This function converts all columns of a data frame to class numeric
for which the conversion does not produce NA
values. Columns for which the conversion results in NA
values are maintained. Optionally, it returns only a logical vector indicating which columns can be converted.
1 2 | asNumericIfPossible(dat, set.numeric = TRUE, transform.factors = FALSE,
maintain.factor.scores = TRUE, verbose = TRUE)
|
dat |
A data frame which should be converted. |
set.numeric |
Logical: If |
transform.factors |
Logical: If |
maintain.factor.scores |
Logical: If |
verbose |
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.
Either a logical vector indicating which columns in the data frame can be converted to class numeric
according to the specified conditions or a data frame in which all possible 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.