Learn R Programming

⚠️There's a newer version (1.0.0) of this package.Take me there.

forcats

CRAN_Status_Badge](https://cran.r-project.org/package=forcats)

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

Installation

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

install.packages("forcats")

Or the development version from github with:

# install.packages("devtools")
devtools::install_github("hadley/forcats")

Key functions:

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.

Copy Link

Version

Install

install.packages('forcats')

Monthly Downloads

722,693

Version

0.1.1

License

GPL-3

Issues

Pull Requests

Stars

Forks

Maintainer

Hadley Wickham

Last Published

September 15th, 2016

Functions in forcats (0.1.1)

fct_expand

Add additional levels to a factor
fct_collapse

Collapse factors into groups.
fct_explicit_na

Make missing values explicit
fct_inorder

Reorders levels in order of first appearance or frequency.
fct_count

Count entries in a factor
fct_c

Concatenate factors, unioning levels.
fct_recode

Change the levels of a factor
fct_lump

Lump together least/most common levels into "other".
fct_anon

Anonymise factor levels
fct_drop

Drop unnused levels
fct_rev

Reverse the levels of a factor
lvls

Low-level functions for manipulating levels
fct_shift

Shift the order of levels of a factor
lvls_union

Find all levels in a list of factors
fct_relevel

Change the order of levels in a factor
fct_shuffle

Randomly permute the levels of a factor
fct_reorder

Reorder the levels of a function according to another variable
fct_unify

Unify the levels in a list of factors
fct_unique

Unique values of a factor
gss_cat

A sample of categorical variables from the General Social survey
%>%

Pipe operator