问题
How do I reorder factor-valued columns by frequency - in increasing order?
While the forcats package provides an explicit way to reorder a factor based on its frequency (fct_infreq()), it does so in decreasing frequency order. I need the reverse order of the factor frequency/counts.
E.g.
library(forcats)
set.seed(555)
df <- data.frame(x=factor(sample(as.character(1:10), 100, replace=TRUE)))
table(df$x)
1 10 2 3 4 5 6 7 8 9
9 10 12 14 10 10 5 12 8 10
levels(fct_infreq(df$x))
[1] "3" "2" "7" "10" "4" "5" "9" "1" "8" "6"
Is there a simple way to flip the ordering so that the least frequent factor ("6") is first and the most frequent ("3") last?
回答1:
This can be done simply by using fct_rev as follows:
levels(fct_rev(fct_infreq(df$x)))
[1] "6" "8" "1" "9" "5" "4" "10" "7" "2" "3"
回答2:
Or, you can do it in base R by sorting and resetting the levels.
xLev = names(table(df$x))[order(table(df$x))]
df$x = factor(df$x, levels=xLev)
table(df$x)
6 8 1 10 4 5 9 2 7 3
5 8 9 10 10 10 10 12 12 14
回答3:
with(data.frame(table(df$x)), setNames(sort(Freq), Var1[order(Freq)]))
# 6 8 1 10 4 5 9 2 7 3
# 5 8 9 10 10 10 10 12 12 14
来源:https://stackoverflow.com/questions/45846965/reorder-factors-by-increasing-frequency