Change order of levels

Keep the values of the levels the same, but change their order. These are particularly useful for models, tables, and visualisations.

fct_relevel

Reorder factor levels by hand

fct_inorder fct_infreq

Reorder factors levels by first appearance or frequency

fct_reorder fct_reorder2

Reorder factor levels by sorting along another variable

fct_shuffle

Randomly permute factor levels

fct_rev

Reverse order of factor levels

fct_shift

Shift factor levels to left or right, wrapping around at end

Change value of levels

Change factor levels, while preserving order (as much as possible).

fct_anon

Anonymise factor levels

fct_collapse

Collapse factor levels into manually defined groups

fct_lump

Lump together least/most common factor levels into "other"

fct_other

Replace levels with "other"

fct_recode

Change factor levels by hand

fct_relabel

Automatically relabel factor levels, collapse as necessary

Add new levels

Leave existing data as is, but add more levels.

fct_expand

Add additional levels to a factor

fct_explicit_na

Make missing values explicit

Other helpers

as_factor

Convert input to a factor.

fct_c

Concatenate factors, combining levels

fct_count

Count entries in a factor

fct_unify

Unify the levels in a list of factors

fct_unique

Unique values of a factor

fct_drop

Drop unused levels

lvls_reorder lvls_revalue lvls_expand

Low-level functions for manipulating levels

lvls_union

Find all levels in a list of factors

Data

gss_cat

A sample of categorical variables from the General Social survey