這是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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。