Learn R Programming

fctutils (version 0.0.7)

ft_substr_freq: Reorder Factor Levels Based on Substring Frequency

Description

Reorders the levels of a factor vector based on the frequency of substrings extracted from the data.

Usage

ft_substr_freq(
  factor_vec,
  start_pos = NULL,
  end_pos = NULL,
  case = FALSE,
  decreasing = TRUE,
  inplace = TRUE
)

Value

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

Arguments

factor_vec

A factor vector whose levels are to be reordered.

start_pos

Positive integer. The starting position of the substring. If NULL, starts from the beginning.

end_pos

Positive integer. The ending position of the substring. If NULL, goes to the end of the string.

case

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

Author

Kai Guo

Examples

Run this code
# Example factor vector with multi-byte characters
factor_vec <- factor(c('apple', 'banana', 'apricot', 'cherry', 'banana', 'banana', 'date'))
# Reorder from position 2 to end
ft_substr_freq(factor_vec, start_pos = 2)
factor_vec <- factor(c('apple', 'banana', 'apricot', 'cherry', 'banana', 'banana', 'date'))
ft_substr_freq(factor_vec, start_pos = 2, end_pos=3)

Run the code above in your browser using DataLab