Unlimited learning, half price | 50% off
Get 50% off unlimited learning

fctutils (version 0.0.7)

ft_regex_freq: Reorder Factor Levels Based on Regex Pattern Frequency

Description

Reorders the levels of a factor vector based on the frequency of substrings matching a regular expression.

Usage

ft_regex_freq(
  factor_vec,
  pattern,
  case = FALSE,
  decreasing = TRUE,
  inplace = TRUE
)

Value

A factor vector with levels reordered based on the frequency of matched substrings.

Arguments

factor_vec

A factor vector whose levels are to be reordered.

pattern

A string representing the regular expression pattern to match.

case

Logical. Should the pattern matching be case-sensitive? Default is FALSE.

decreasing

Logical. Should the ordering be decreasing by frequency? 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', 'apricot', 'cherry', 'blueberry', 'blackberry', 'date'))

# Reorder based on pattern matching 'a'
ft_regex_freq(factor_vec, pattern = 'a')

# Reorder with case-sensitive matching
ft_regex_freq(factor_vec, pattern = '^[A-Z]', case = TRUE)

Run the code above in your browser using DataLab