Learn R Programming

dae (version 3.2.30)

as.numfac: Convert a factor to a numeric vector, possibly centering or scaling the values

Description

Converts a factor to a numeric vector with approximately the numeric values of its levels. Hence, the levels of the factor must be numeric values, stored as characters. It uses the method described in factor. Use as.numeric to convert a factor to a numeric vector with integers 1, 2, ... corresponding to the positions in the list of levels. The numeric values can be centred and/or scaled. You can also use fac.recast to recode the levels to numeric values. If a numeric is supplied and both center and scale are FALSE, it is left unchanged; otherwise, it will be centred and scaled according to the settings of center and scale.

Usage

as.numfac(factor, center = FALSE, scale = FALSE)

Value

A numeric vector. An NA will be stored for any value of the factor whose level is not a number.

Arguments

factor

The factor to be converted.

center

Either a logical value or a numeric-alike value, where numeric-alike means that as.numeric will be applied successfully if is.numeric is not TRUE.

scale

Either a logical value, a numeric-alike vector, where numeric-alike means that as.numeric will be applied successfully if is.numeric is not TRUE.

Author

Chris Brien

Details

The value of center specifies how the centring is done. If it is TRUE, the mean of the unique values of the factor are subtracted, after the factor is converted to a numeric. If center is numeric, it is subtracted from factor's converted numeric values. If center is FALSE no scaling is done.

The value of scale specifies how scaling is performed, after any centring is done. If scale is TRUE, the unique converted values of the factor are divided by (i) the standard deviaton when the values have been centred and (ii) the root-mean-square error if they have not been centred; the root-mean-square is given by \(\sqrt{\Sigma(x^2)/(n-1)}\), where x contains the unique converted factor values and n is the number of unique values. If scale is FALSE no scaling is done.

See Also

as.numeric, fac.recast in package dae, factor, scale.

Examples

Run this code
## set up a factor and convert it to a numeric vector
a <- factor(rep(1:6, 4))
x <- as.numfac(a)
x <- as.numfac(a, center = TRUE)
x <- as.numfac(a, center = TRUE, scale = TRUE)

Run the code above in your browser using DataLab