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 factor to be recast.

newlevels

A vector of length levels(factor) that changes both the values in the factor vector and its levels attribute. The values in the newlevels vector need not be unique, but there must be as many values as there are levels in the supplied factor. The levels in the vector of the supplied factor that have the same value in newlevels will be combined in the recast factor. The values in the new levels attribute can be re-oredered using levels.order.

levels.order

A vector that specifies the order of the levels in the levels attribute of the recast factor. If newlevels is NULL, must be of length levels(factor) and contain the old levels in the new order for the recast factor. If newlevels is not NULL, the vector must be of length(unique(newlevels)) and contain the unique values in newlevels in the new order for the recast factor. The values in the factor vector whose levels are being re-ordered will be unchanged. If levels.order is NULL, then the current levels attribute of factor is used.

newlabels

A vector of length levels(factor) if newlevels is NULL, and of length
unique(newlevels) if it is not NULL. It should contain the values to be used as labels in the recast factor. Effectively, this changes the values in the factor vector to those given in newlabels and the levels attribute to newlabels.

...

Further arguments passed to the factor call creating the new factor.

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))


[Package dae version 3.2-13 Index]