reorderLevels {iNZightTools}R Documentation

Reorder a categorical

Description

Reorder the factors of a categorical variable either manually or frequency

Usage

reorderLevels(
  .data,
  var,
  new_levels = NULL,
  freq = FALSE,
  name = sprintf("%s.reord", var)
)

Arguments

.data

a dataframe to reorder

var

a categorical variable to reorder

new_levels

a character vector of the new factor order. Only specify if freq = FALSE

freq

logical, If freq = FALSE (default), will manually reorder using new_levels. If freq = TRUE, will reorder based of descending frequency of the factor levels

name

name for the new variable

Value

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

Author(s)

Owen Jin

See Also

code

Examples

Run examples

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


[Package iNZightTools version 1.12.2 Index]