asNumericIfPossible: Convert Columns of a Data Frame Into Numeric Values If...

Description Usage Arguments Details Value Author(s) Examples

Description

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.

Usage

1
2
asNumericIfPossible(dataFrame, set.numeric=TRUE, transform.factors=FALSE, maintain.factor.scores = TRUE,
      verbose=TRUE, ignoreAttributes = FALSE)

Arguments

dataFrame

A data frame which should be converted.

set.numeric

Logical: If TRUE, a data frame with all convertible columns converted to class numeric is returned. If FALSE, a logical vector indicating which columns are convertible to class numeric.

transform.factors

Logical indicating whether columns of class factor should be converted. If FALSE, columns of class factor are maintained. If TRUE, conversion of factors is attempted.

maintain.factor.scores

Logical: If TRUE, conversion of the factor levels is attempted (like in as.numeric(as.character(f))). If FALSE, the internal codes of the factor are returned (like in as.numeric(f)). See 'Details'. This argument is only evaluated if transform.factors = TRUE.

verbose

Logical: If TRUE, information about the class of the columns in the data.frame is given on the console.

ignoreAttributes

Logical: If TRUE, variable attributes are tried to restore after transformation.

Details

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.

Value

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.

Author(s)

Sebastian Weirich

Examples

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)

eatRep documentation built on May 2, 2019, 5:40 p.m.