AsNumeric: Coerce categorical data to numeric or logical to integer

View source: R/asnumeric.R

AsNumericR Documentation

Coerce categorical data to numeric or logical to integer

Description

Coerces a data.frame or vector that may contain factors into a vector or data.frame that does not contain factors.

Usage

AsNumeric(x, binary = TRUE, name = NULL, remove.first = FALSE)

Arguments

x

The data.frame or vector.

binary

If TRUE, unordered factors are represented as dummy variables. Otherwise, their value attributes/levels are used to coerce each factor to a single numeric variable. See the Details.

name

Used if binary is TRUE to construct variable names. This parameter is ignored if x is a data.frame or a list.

remove.first

Remove the first binary variable.

Details

Characters are treated as factors.

When binary is FALSE, it is first checked if x is a variable set (question) from Displayr (Q), in which case their value attributes are used. To check this, (x is searched for attributes "questiontype", and "sourcevalues", "values", "codeframe" (for "PickOne" questiontype/vector x) and "sourcevariablevalues", "variablevalues", and "codeframe" attributes, in the case of "PickOneMulti" questiontype/data.frame x). See the Examples.

If x is missing these attributes, Unclass is used. If all labels of x (or columns of x) can be coerced to numeric, these values will be used to create the numeric variable; otherwise, the integers 1, 2, ... are used for the values.

See Also

numbersFromCategoricalVariableSets, Unclass, FactorToNumeric

Examples

file <- system.file("extdata", "variable.sets.rda", package = "flipTransformations")
vs.env <- new.env()
load(file, vs.env)
dummy.nm <- AsNumeric(vs.env$nominal.multi, TRUE)
num.nm <- AsNumeric(vs.env$nominal.merge.hide, FALSE)
## Compare
table(num.nm)
levels(vs.env$nominal.merge.hide)

Displayr/flipTransformations documentation built on Feb. 26, 2024, 12:47 a.m.