这是stats::relevel()
的概括,允许您将任意数量的级别移动到任意位置。
参数
- .f
-
因子(或字符向量)。
- ...
-
函数(或公式)或字符级别。
将使用当前级别作为输入来调用函数,并且返回值(必须是字符向量)将用于重新调整因子。
任何未提及的级别将保留其现有顺序,默认情况下位于明确提及的级别之后。支持整齐的点。
- after
-
新值应该放在哪里?
例子
f <- factor(c("a", "b", "c", "d"), levels = c("b", "c", "d", "a"))
fct_relevel(f)
#> [1] a b c d
#> Levels: b c d a
fct_relevel(f, "a")
#> [1] a b c d
#> Levels: a b c d
fct_relevel(f, "b", "a")
#> [1] a b c d
#> Levels: b a c d
# Move to the third position
fct_relevel(f, "a", after = 2)
#> [1] a b c d
#> Levels: b c a d
# Relevel to the end
fct_relevel(f, "a", after = Inf)
#> [1] a b c d
#> Levels: b c d a
fct_relevel(f, "a", after = 3)
#> [1] a b c d
#> Levels: b c d a
# Relevel with a function
fct_relevel(f, sort)
#> [1] a b c d
#> Levels: a b c d
fct_relevel(f, sample)
#> [1] a b c d
#> Levels: b a c d
fct_relevel(f, rev)
#> [1] a b c d
#> Levels: a d c b
# Using 'Inf' allows you to relevel to the end when the number
# of levels is unknown or variable (e.g. vectorised operations)
df <- forcats::gss_cat[, c("rincome", "denom")]
lapply(df, levels)
#> $rincome
#> [1] "No answer" "Don't know" "Refused" "$25000 or more"
#> [5] "$20000 - 24999" "$15000 - 19999" "$10000 - 14999" "$8000 to 9999"
#> [9] "$7000 to 7999" "$6000 to 6999" "$5000 to 5999" "$4000 to 4999"
#> [13] "$3000 to 3999" "$1000 to 2999" "Lt $1000" "Not applicable"
#>
#> $denom
#> [1] "No answer" "Don't know" "No denomination"
#> [4] "Other" "Episcopal" "Presbyterian-dk wh"
#> [7] "Presbyterian, merged" "Other presbyterian" "United pres ch in us"
#> [10] "Presbyterian c in us" "Lutheran-dk which" "Evangelical luth"
#> [13] "Other lutheran" "Wi evan luth synod" "Lutheran-mo synod"
#> [16] "Luth ch in america" "Am lutheran" "Methodist-dk which"
#> [19] "Other methodist" "United methodist" "Afr meth ep zion"
#> [22] "Afr meth episcopal" "Baptist-dk which" "Other baptists"
#> [25] "Southern baptist" "Nat bapt conv usa" "Nat bapt conv of am"
#> [28] "Am bapt ch in usa" "Am baptist asso" "Not applicable"
#>
df2 <- lapply(df, fct_relevel, "Don't know", after = Inf)
lapply(df2, levels)
#> $rincome
#> [1] "No answer" "Refused" "$25000 or more" "$20000 - 24999"
#> [5] "$15000 - 19999" "$10000 - 14999" "$8000 to 9999" "$7000 to 7999"
#> [9] "$6000 to 6999" "$5000 to 5999" "$4000 to 4999" "$3000 to 3999"
#> [13] "$1000 to 2999" "Lt $1000" "Not applicable" "Don't know"
#>
#> $denom
#> [1] "No answer" "No denomination" "Other"
#> [4] "Episcopal" "Presbyterian-dk wh" "Presbyterian, merged"
#> [7] "Other presbyterian" "United pres ch in us" "Presbyterian c in us"
#> [10] "Lutheran-dk which" "Evangelical luth" "Other lutheran"
#> [13] "Wi evan luth synod" "Lutheran-mo synod" "Luth ch in america"
#> [16] "Am lutheran" "Methodist-dk which" "Other methodist"
#> [19] "United methodist" "Afr meth ep zion" "Afr meth episcopal"
#> [22] "Baptist-dk which" "Other baptists" "Southern baptist"
#> [25] "Nat bapt conv usa" "Nat bapt conv of am" "Am bapt ch in usa"
#> [28] "Am baptist asso" "Not applicable" "Don't know"
#>
# You'll get a warning if the levels don't exist
fct_relevel(f, "e")
#> Warning: 1 unknown level in `f`: e
#> [1] a b c d
#> Levels: b c d a
相关用法
- R forcats fct_relabel 使用函数重新标记因子水平,并根据需要折叠
- R forcats fct_rev 因子水平的倒序
- R forcats fct_recode 手动更改因子水平
- R forcats fct_reorder 通过沿另一个变量排序来重新排序因子水平
- R forcats fct_anon 匿名因子水平
- R forcats fct_inorder 按首次出现、频率或数字顺序对因子水平重新排序
- R forcats fct_match 测试因子中是否存在水平
- R forcats fct_drop 删除未使用的级别
- R forcats fct_c 连接因子,组合级别
- R forcats fct_collapse 将因子级别折叠为手动定义的组
- R forcats fct_shuffle 随机排列因子水平
- R forcats fct_cross 组合两个或多个因子的水平以创建新因子
- R forcats fct_other 手动将级别替换为“其他”
- R forcats fct_na_value_to_level NA 值和 NA 水平之间的转换
- R forcats fct_lump 将不常见因子集中到“其他”级别
- R forcats fct_unique 一个因子的唯一值,作为一个因子
- R forcats fct_shift 将因子水平向左或向右移动,在末尾环绕
- R forcats fct_unify 统一因子列表中的水平
- R forcats fct_count 计算因子中的条目数
- R forcats fct_expand 向因子添加附加级别
- R forcats fct 创建一个因子
- R forcats as_factor 将输入转换为因子
- R forcats lvls_union 查找因子列表中的所有级别
- R forcats lvls 用于操纵级别的低级函数
- R forcats gss_cat 一般社会调查中的分类变量样本
注:本文由纯净天空筛选整理自Hadley Wickham等大神的英文原创作品 Reorder factor levels by hand。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。