Reorder and combine levels of a factor

Share:

Description

The levels of a factor are re-ordered so that the levels specified by ref is first and the others are moved down. This is useful for contr.treatment contrasts which take the first level as the reference. Levels may also be combined.

Usage

1
2
## S3 method for class 'factor'
Relevel( x, ref, first = TRUE, collapse="+", ... )

Arguments

x

An unordered factor

ref

The names or numbers of levels to be the first. If ref is a list, factor levels mentioned in each list element are combined. If the list is named the names are used as new factor levels.

first

Should the levels mentioned in ref come before those not?

collapse

String used when collapsing factor levels.

...

Arguments passed on to other methods.

Value

An unordered factor, where levels of x have been reordered and/or collapsed.

Author(s)

Bendix Carstensen, BendixCarstensen.com.

See Also

Relevel.Lexis

Examples

1
2
3
ff <- factor( sample( letters[1:5], 100, replace=TRUE ) )
table( ff, Relevel( ff, list( AB=1:2, "Dee"=4, c(3,5) ) ) )
table( ff, rr=Relevel( ff, list( 5:4, Z=c("c","a") ), coll="-und-", first=FALSE ) )

Want to suggest features or report bugs for rdrr.io? Use the GitHub issue tracker.