reorder_levels: Reorder the levels of a categorical variable

View source: R/mutate.R

reorder_levelsR Documentation

Reorder the levels of a categorical variable

Description

Reorder the levels of a categorical variable either manually or automatically

Usage

reorder_levels(
  data,
  var,
  new_levels = NULL,
  auto = c("freq", "order", "seq"),
  name = NULL
)

Arguments

data

a dataframe to reorder

var

a categorical variable to reorder

new_levels

a character vector of the new factor order; overrides auto if not NULL

auto

only meaningful if new_levels is NULL: the method to auto-reorder the levels, see fct_inorder

name

name for the new variable

Value

original dataframe containing a new column of the reordered categorical variable with tidyverse code attached

Author(s)

Zhaoming Su

See Also

code

Examples

reordered <- reorder_levels(iris,
    var = "Species",
    new_levels = c("versicolor", "virginica", "setosa")
)
cat(code(reordered))
head(reordered)

reordered <- reorder_levels(iris,
    var = "Species",
    auto = "freq"
)
cat(code(reordered))
head(reordered)


iNZightTools documentation built on Oct. 12, 2023, 5:06 p.m.