Learn R Programming

iNZightTools (version 1.13.0)

reorderLevels: 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)
)

Value

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

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

Author

Owen Jin

See Also

code

Examples

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

Run the code above in your browser using DataLab