Learn R Programming

fctutils (version 0.0.7)

ft_count: Reorder Factor Levels by Level Count

Description

Reorders the levels of a factor vector based on the count of each level in the data.

Usage

ft_count(factor_vec, decreasing = TRUE, inplace = FALSE)

Value

A factor vector with levels reordered based on their count.

Arguments

factor_vec

A factor vector whose levels are to be reordered.

decreasing

Logical. Should the ordering be decreasing by count? Default is TRUE.

inplace

Logical. If TRUE, returns a new factor vector with elements reordered to align with the new levels' order. If FALSE, returns a new factor vector with only the levels' order adjusted, leaving the data vector's elements' order unchanged. Defaults to FALSE.

Author

Kai Guo

Examples

Run this code
# Example factor vector
factor_vec <- factor(c('apple', 'banana', 'apple', 'cherry', 'banana', 'banana', 'date'))

# Reorder levels by decreasing count
ft_count(factor_vec)

# Reorder levels by increasing count
ft_count(factor_vec, decreasing = FALSE)

Run the code above in your browser using DataLab