R: Drop unused factor levels

drop.levels {gdata} | R Documentation

## Drop unused factor levels

### Description

Drop unused levels in a factor

### Usage

    drop.levels(x, reorder=TRUE, ...)

### Arguments

`x` | object to be processed
`reorder` | should factor levels be reordered using `reorder.factor`?
`...` | additional arguments to `reorder.factor`

### Details

`drop.levels` is a generic function, where default method does nothing, while method for factor `s` drops all unused levels. Drop is done with `x[, drop=TRUE]`.

There are also convenient methods for `list` and `data.frame`, where all unused levels are dropped in all factors (one by one) in a `list` or a `data.frame`.

### Value

Input object without unused levels.

### Author(s)

Jim Rogers james.a.rogers@pfizer.com and Gregor Gorjanc

### Examples

    f <- factor(c("A", "B", "C", "D"))[1:3]
    drop.levels(f)

    l <- list(f=f, i=1:3, c=c("A", "B", "D"))
    drop.levels(l)

    df <- as.data.frame(l)
    str(df)
    str(drop.levels(df))

[Package gdata version 2.18.0 Index]