droplevels: droplevels

Description Usage Arguments Details Value Note See Also Examples

View source: R/factor.R

Description

The function droplevels is used to drop unused levels from a factor or, more commonly, from factors in a data frame.

Usage

1
2
3
4
## S3 method for class 'factor'
droplevels(x, ...)
## S3 method for class 'data.frame'
droplevels(x, except, ...)

Arguments

x

an object from which to drop unused factor levels.

...

further arguments passed to methods

except

indices of columns from which not to drop levels

Details

The method for class "factor" is essentially equivalent to factor(x).

The except argument follow the usual indexing rules.

Value

droplevels returns an object of the same class as x

Note

This function was introduced in R 2.12.0. It is primarily intended for cases where one or more factors in a data frame contains only elements from a reduced level set after subsetting. (Notice that subsetting does not in general drop unused levels). By default, levels are dropped from all factors in a data frame, but the except argument allows you to specify columns for which this is not wanted.

See Also

subset for subsetting data frames. factor for definition of factors. drop for dropping array dimensions. drop1 for dropping terms from a model. [.factor for subsetting of factors.

Examples

1
2
3
4
aq <- transform(airquality, Month = factor(Month, labels = month.abb[5:9]))
aq <- subset(aq, Month != "Jul")
table(aq$Month)
table(droplevels(aq)$Month)

robertzk/monadicbase documentation built on May 27, 2019, 10:35 a.m.