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

Description Usage Arguments Details Value Author(s) Examples

View source: R/asNumericIfPossible.R

Description

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.

Usage

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

Arguments

dat

A data frame which should be converted.

set.numeric

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

transform.factors

Logical: If FALSE, columns of class factor are maintained. If TRUE, conversion of factors is attempted.

maintain.factor.scores

Logical: If TRUE, the factor levels are converted (as in as.numeric(as.character(f))). If FALSE, the internal codes of the factor are converted (as in as.numeric(f)). 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.

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.

Value

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.

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)

eatPrep documentation built on May 2, 2019, 5:20 p.m.