# Example 1: Reorder levels based on character frequency without reordering data elements
factor_vec <- factor(c('apple', 'banana', 'cherry', 'date', 'fig', 'grape'))
new <- ft_freq(
factor_vec,
case = FALSE,
decreasing = TRUE,
inplace = FALSE
)
print(new)
# [1] apple banana cherry date fig grape
# Levels: apple banana date cherry fig grape
# Example 2: Reorder levels based on character frequency and reorder data elements
new_inplace <- ft_freq(
factor_vec,
case = FALSE,
decreasing = TRUE,
inplace = TRUE
)
print(new_inplace)
# [1] apple banana date cherry fig grape
# Levels: apple banana date cherry fig grape
# Example 3: Reorder levels in decreasing order based on character frequency
# without reordering data elements
new_dec <- ft_freq(
factor_vec,
case = FALSE,
decreasing = TRUE,
inplace = FALSE
)
print(new_dec)
# [1] apple banana cherry date fig grape
# Levels: apple banana date cherry fig grape
# Example 4: Reorder levels with case sensitivity and reorder data elements
factor_vec_case <- factor(c('Apple', 'banana', 'Cherry', 'date', 'Fig', 'grape'))
new_case <- ft_freq(
factor_vec_case,
case = TRUE,
decreasing = TRUE,
inplace = TRUE
)
print(new_case)
# [1] Apple banana Cherry date Fig grape
# Levels: cherry Apple banana grape Fig date
# Example 5: Reorder levels based on character frequency, allowing insertion beyond string length
factor_vec_short <- factor(c('go', 'dog', 'cat', 'bird'))
new_short <- ft_freq(
factor_vec_short,
case = FALSE,
decreasing = TRUE,
inplace = FALSE
)
print(new_short)
# [1] go dog cat bird
# Levels: cat dog bird go
Run the code above in your browser using DataLab