Tools for Working with Categorical Variables (Factors)

Helpers for reordering factor levels (including moving specified levels to front, ordering by first appearance, reversing, and randomly shuffling), and tools for modifying factor levels (including collapsing rare levels into other, 'anonymising', and manually 'recoding').


forcats provides tools for categorical variables (forcats is an anagram of factors).

You can install the released version of forcats from CRAN with:


Or the development version from github with:

# install.packages("devtools")

Change order of levels:

Change value of levels:

Add new levels:

A few other helpers:

  • fct_c(): concatenate factors using union of levels.

  • fct_count():
    count occurences of levels, optionally sorting by frequency.

  • fct_unify():
    ensure list of factors share the same levels.

  • fct_unique(): compute from levels of factor.

  • fct_drop(): drop levels without data (same as base::droplevels()).

  • lvls_union(): finds union of levels from list of factors.


forcats 0.1.1

  • Minor fixes for R CMD check

  • Add package docs

Reference manual

It appears you don't have a PDF plugin for this browser. You can click here to download the reference manual.


0.2.0 by Hadley Wickham, 2 months ago,

Report a bug at

Browse source code at

Authors: Hadley Wickham [aut, cre], RStudio [cph, fnd]

Documentation:   PDF Manual  

GPL-3 license

Imports tibble, magrittr

Suggests ggplot2, testthat, covr

Imported by tidyverse.

Suggested by nzelect, tidyquant.

See at CRAN