用任意數字標識符替換因子水平。級別的值和順序都不會保留。
例子
gss_cat$relig %>% fct_count()
#> # A tibble: 16 × 2
#> f n
#> <fct> <int>
#> 1 No answer 93
#> 2 Don't know 15
#> 3 Inter-nondenominational 109
#> 4 Native american 23
#> 5 Christian 689
#> 6 Orthodox-christian 95
#> 7 Moslem/islam 104
#> 8 Other eastern 32
#> 9 Hinduism 71
#> 10 Buddhism 147
#> 11 Other 224
#> 12 None 3523
#> 13 Jewish 388
#> 14 Catholic 5124
#> 15 Protestant 10846
#> 16 Not applicable 0
gss_cat$relig %>%
fct_anon() %>%
fct_count()
#> # A tibble: 16 × 2
#> f n
#> <fct> <int>
#> 1 01 224
#> 2 02 93
#> 3 03 689
#> 4 04 95
#> 5 05 23
#> 6 06 15
#> 7 07 32
#> 8 08 104
#> 9 09 10846
#> 10 10 71
#> 11 11 3523
#> 12 12 0
#> 13 13 5124
#> 14 14 147
#> 15 15 109
#> 16 16 388
gss_cat$relig %>%
fct_anon("X") %>%
fct_count()
#> # A tibble: 16 × 2
#> f n
#> <fct> <int>
#> 1 X01 147
#> 2 X02 10846
#> 3 X03 95
#> 4 X04 23
#> 5 X05 689
#> 6 X06 0
#> 7 X07 224
#> 8 X08 93
#> 9 X09 71
#> 10 X10 104
#> 11 X11 15
#> 12 X12 109
#> 13 X13 388
#> 14 X14 3523
#> 15 X15 5124
#> 16 X16 32
相關用法
- R forcats fct_relevel 手動重新排序因子級別
- R forcats fct_inorder 按首次出現、頻率或數字順序對因子水平重新排序
- R forcats fct_rev 因子水平的倒序
- R forcats fct_match 測試因子中是否存在水平
- R forcats fct_relabel 使用函數重新標記因子水平,並根據需要折疊
- 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_recode 手動更改因子水平
- 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_reorder 通過沿另一個變量排序來重新排序因子水平
- R forcats fct 創建一個因子
- R forcats as_factor 將輸入轉換為因子
- R forcats lvls_union 查找因子列表中的所有級別
- R forcats lvls 用於操縱級別的低級函數
- R forcats gss_cat 一般社會調查中的分類變量樣本
注:本文由純淨天空篩選整理自Hadley Wickham等大神的英文原創作品 Anonymise factor levels。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。