R 通过增加频率对因子进行重新排序
如何按频率对因子值列重新排序-按递增顺序 虽然forcats包提供了一种基于其频率()对因子重新排序的显式方法,但它是按频率递减的顺序进行排序的。我需要因子频率/计数的相反顺序 例如R 通过增加频率对因子进行重新排序,r,tidyverse,R,Tidyverse,如何按频率对因子值列重新排序-按递增顺序 虽然forcats包提供了一种基于其频率()对因子重新排序的显式方法,但它是按频率递减的顺序进行排序的。我需要因子频率/计数的相反顺序 例如 库(用于猫) 种子集(555) df只需使用以下方法即可完成: levels(fct_rev(fct_infreq(df$x))) [1] "6" "8" "1" "9" "5" "4" "10" "7" "2" "3" 或者,您可以在BaseR中通过排序和重置级别来执行此操作 xLev =
库(用于猫)
种子集(555)
df只需使用以下方法即可完成:
levels(fct_rev(fct_infreq(df$x)))
[1] "6" "8" "1" "9" "5" "4" "10" "7" "2" "3"
或者,您可以在BaseR中通过排序和重置级别来执行此操作
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
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
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