R: Drop unused levels

fct_drop {forcats} | R Documentation

## Drop unused levels

### Description

Compared to `base::droplevels()` does not drop `NA` levels that have values.

### Usage

    fct_drop(f, only)

### Arguments

`f` | A factor
`only` | A character vector restricting the set of levels to be dropped. If supplied, only levels that have no entries and appear in this vector will be removed.

### Examples

    f <- factor(c("a", "b"), levels = c("a", "b", "c"))
    f
    fct_drop(f)

    # Set only to restrict which levels to drop
    fct_drop(f, only = "a")
    fct_drop(f, only = "c")

[Package forcats version 0.2.0 Index]