ft_reorder_within | R Documentation |
Reorders the levels of a factor vector within groups defined by another factor vector.
ft_reorder_within(factor_vec, group_vec, by, fun = mean, decreasing = FALSE)
factor_vec |
A factor vector to be reordered. |
group_vec |
A factor vector defining the groups. |
by |
A numeric vector to order by. |
fun |
A function to summarize within groups (e.g., mean, median). |
decreasing |
Logical. Should the ordering be decreasing? Default is |
A factor vector with levels reordered within groups.
Kai Guo
# Example data
data <- data.frame(
item = factor(c('A', 'B', 'C', 'D', 'E', 'F')),
group = factor(c('G1', 'G1', 'G1', 'G2', 'G2', 'G2')),
value = c(10, 15, 5, 20, 25, 15)
)
data <- rbind(data, data)
# Reorder 'item' within 'group' by 'value'
data$item <- ft_reorder_within(data$item, data$group, data$value, mean)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.