
Reorders the levels of a factor vector based on the frequency of substrings matching a regular expression.
ft_regex_freq(
factor_vec,
pattern,
case = FALSE,
decreasing = TRUE,
inplace = TRUE
)
A factor vector with levels reordered based on the frequency of matched substrings.
A factor vector whose levels are to be reordered.
A string representing the regular expression pattern to match.
Logical. Should the pattern matching be case-sensitive? Default is FALSE
.
Logical. Should the ordering be decreasing by frequency? Default is TRUE
.
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
.
Kai Guo
# 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