fac.recast {dae} | R Documentation |
Recasts a factor by modifying the values in the factor vector and/or the levels attribute, possibly combining some levels into a single level.
Description
A factor
is comprised of a vector of values and a levels
attribute.
This function can modify these separately or jointly. The newlevels
argument recasts
both the values of a factor
vector and the levels
attribute, using each
value in the newlevels
vector to replace the corresponding value in both factor
vector and the levels
attribute. The factor
, possibly
with the new levels, can have its levels
attribute reordered and/or new
labels
associated with the levels
using the levels.order
and newlabels
arguments.
Usage
fac.recast(factor, newlevels = NULL, levels.order = NULL, newlabels = NULL, ...)
Arguments
factor |
The |
newlevels |
A |
levels.order |
A |
newlabels |
A |
... |
Further arguments passed to the |
Value
A factor
.
Author(s)
Chris Brien
See Also
fac.uselogical, as.numfac
and mpone
in package dae,
factor
, relevel
.
Examples
## set up a factor with labels
Treats <- factor(rep(1:4, 4), labels=letters[1:4])
## recast to reduce the levels: "a" and "d" to 1 and "b" and "c" to 2, i.e. from 4 to 2 levels
A <- fac.recast(Treats, newlevels = c(1,2,2,1), labels = letters[1:2])
A <- fac.recast(Treats, newlevels = letters[c(1,2,2,1)])
#reduce the levels from 4 to 2, with re-ordering the levels vector without changing the values
#of the new recast factor vector
A <- fac.recast(Treats, newlevels = letters[c(1,2,2,1)], levels.order = letters[2:1])
#reassign the values in the factor vector without re-ordering the levels attribute
A <- fac.recast(Treats, newlevels = letters[4:1])
#reassign the values in the factor vector, with re-ordering the levels attribute
A <- fac.recast(Treats, newlabels = letters[4:1])
#reorder the levels attribute with changing the values in the factor vector
A <- fac.recast(Treats, levels.order = letters[4:1])
#reorder the values in the factor vector without changing the levels attribute
A <- fac.recast(Treats, newlevels = 4:1, newlabels = levels(Treats))