ft_char_freq: Reorder Factor Levels Based on Character Frequency at...

View source: R/single_fct.R

ft_char_freqR Documentation

Reorder Factor Levels Based on Character Frequency at Positions

Description

Reorders the levels of a factor vector based on the frequency of characters at specified positions within the data.

Usage

ft_char_freq(
  factor_vec,
  positions,
  case = FALSE,
  decreasing = TRUE,
  inplace = TRUE
)

Arguments

factor_vec

A factor vector whose levels are to be reordered.

positions

A vector of positive integers specifying the character positions to consider.

case

Logical. Should the character comparison 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.

Value

A factor vector with levels reordered based on the frequency of characters at specified positions.

Author(s)

Kai Guo

Examples

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

# Reorder based on characters at positions 1 and 2
ft_char_freq(factor_vec, positions = 1:2)

# Reorder, case-sensitive, decreasing order
ft_char_freq(factor_vec, positions = c(1, 3), case = TRUE)

fctutils documentation built on Oct. 1, 2024, 1:07 a.m.