as.numfac | R Documentation |
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
.
as.numfac(factor, center = FALSE, scale = FALSE)
factor |
The |
center |
Either a |
scale |
Either a |
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.
A numeric vector
. An NA
will be stored for any value of the factor whose
level is not a number.
Chris Brien
as.numeric
, fac.recast
in package dae,
factor
, scale
.
## 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)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.