
Reorders the levels of a factor vector based on substrings extracted from the factor levels.
ft_sub(
factor_vec,
start_pos = NULL,
end_pos = NULL,
case = FALSE,
decreasing = FALSE,
inplace = TRUE
)
A factor vector with levels reordered based on the specified substring.
A factor vector whose levels are to be reordered.
Positive integer. The starting position of the substring. If NULL
, starts from the beginning.
Positive integer. The ending position of the substring. If NULL
, goes to the end of the string.
Logical. Should the substring comparison be case-sensitive? Default is FALSE
.
Logical. Should the ordering be decreasing? Default is FALSE
.
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', 'Cherry', 'date', 'Fig', 'grape'))
# Reorder based on substring from position 2 to 4
ft_sub(factor_vec, start_pos = 2, end_pos = 4)
# Reorder from position 3 to end, case-sensitive
ft_sub(factor_vec, start_pos = 3, case = TRUE)
Run the code above in your browser using DataLab