Relevel: Reorder and combine levels of a factor

Description Usage Arguments Details Value Author(s) See Also Examples

View source: R/Relevel.R

Description

The levels of a factor are re-ordered so that the levels specified by ref appear first and remaining levels are moved down. This is useful for contr.treatment contrasts which take the first level as the reference. Factor levels may also be combined; two possibilities for specifying this are supported.

Usage

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

Arguments

x

An unordered factor

ref

Vector, list, matrix or table. If ref is a vector (integer or character), it is assumed it contains the names or numbers of levels to be the first ones; nom mentoned levels are kept. 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, otherwise new level names are constructed. If ref is a matrix or table, the two first columns are assumed to have unique levels of x and groupings of this, respectively.

first

Should the levels mentioned in ref (if it is a list) come before those not?

collapse

String used when constructing names for combined factor levels.

xlevels

Logical. Should all levels in the 2nd column of ref be maintained, or (if FALSE) only the acutally ocurring. See details.

nogroup

Logical. Should levels present in the input but not in the 1st column of ref be maintained as levels after the grouping? If FALSE), NAs will be returned for such elements.

...

Arguments passed on to other methods.

Details

The facility where ref is a two-column matrix mimicks the SAS-facility where a dataset can be used to construct a format — SAS format is the grouping tool for variable values. Using this facility will preserve the order of levels if ref[,2] is a factor.

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
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# Grouping using a list (hardcoding)
#
ff <- factor( sample( letters[1:5], 100, replace=TRUE ) )
table( ff, Relevel( ff, list( AB=1:2, "Dee"=4, c(3,5) ) ) )
table( ff, Relevel( ff, list( 5:4, Z=c("c","a") ), coll="-und-",first=FALSE ) )

## Grouping using a two-column matrix as input:
## A factor with levels to be grouped together
ff <- factor( c("Bear","Bear","Crocodile","Snake","Crocodile","Bear") )
ff
## A grouping table
( gg <- data.frame( Animal=c("Bear","Whale","Crocodile","Snake","Eagle"),
                     Class=c("Mammal","Mammal","Reptile","Reptile","Bird") ) )
Relevel( ff, gg, xlevels=FALSE )
Relevel( ff, gg )
Relevel( ff, gg[c(1:5,5:1),] )
## This crashes with an error
## Relevel( ff, rbind( gg, c("Bear","Reptile") ) ) 
ff <- factor(c(as.character(ff),"Jellyfish","Spider"))
Relevel( ff, gg )
Relevel( ff, gg, xlevels=FALSE )
# Note: returns NAs
Relevel( ff, gg, nogroup=FALSE )

Epi documentation built on Nov. 28, 2017, 3:01 a.m.

Related to Relevel in Epi...