ft_len | R Documentation |
Reorders the levels of a factor vector based on the character length of each level. Optionally reorders the data vector's elements to align with the new levels' order.
ft_len(factor_vec, decreasing = FALSE, inplace = FALSE)
factor_vec |
A factor vector to be sorted. |
decreasing |
Logical. Should the ordering be decreasing by length? Default is |
inplace |
Logical. If |
A factor vector with levels reordered based on their length. Depending on the inplace
parameter, the data vector's elements may also be reordered.
Kai Guo
# Example factor vector
factor_vec <- factor(c('apple', 'banana', 'cherry', 'date'))
# Sort levels by length without reordering data elements
sorted_factor <- ft_len(factor_vec)
print(sorted_factor)
# [1] apple banana cherry date
# Levels: apple date banana cherry
# Sort levels by length and reorder data elements
sorted_factor_inplace <- ft_len(factor_vec, inplace = TRUE)
print(sorted_factor_inplace)
# [1] date apple banana cherry
# Levels: apple date banana cherry
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.