使用函數重新標記因子水平,並根據需要折疊
參數
- .f
-
因子(或字符向量)。
- .fun
-
應用於每個級別的函數。必須接受一個字符參數並返回與其輸入長度相同的字符向量。
您還可以使用
~
來創建簡寫(以 purrr 的風格)。~ paste(., "x")
相當於function(.) paste(., "x")
- ...
-
fun
的附加參數。
例子
gss_cat$partyid %>% fct_count()
#> # A tibble: 10 × 2
#> f n
#> <fct> <int>
#> 1 No answer 154
#> 2 Don't know 1
#> 3 Other party 393
#> 4 Strong republican 2314
#> 5 Not str republican 3032
#> 6 Ind,near rep 1791
#> 7 Independent 4119
#> 8 Ind,near dem 2499
#> 9 Not str democrat 3690
#> 10 Strong democrat 3490
gss_cat$partyid %>%
fct_relabel(~ gsub(",", ", ", .x)) %>%
fct_count()
#> # A tibble: 10 × 2
#> f n
#> <fct> <int>
#> 1 No answer 154
#> 2 Don't know 1
#> 3 Other party 393
#> 4 Strong republican 2314
#> 5 Not str republican 3032
#> 6 Ind, near rep 1791
#> 7 Independent 4119
#> 8 Ind, near dem 2499
#> 9 Not str democrat 3690
#> 10 Strong democrat 3490
convert_income <- function(x) {
regex <- "^(?:Lt |)[$]([0-9]+).*$"
is_range <- grepl(regex, x)
num_income <- as.numeric(gsub(regex, "\\1", x[is_range]))
num_income <- trunc(num_income / 5000) * 5000
x[is_range] <- paste0("Gt $", num_income)
x
}
fct_count(gss_cat$rincome)
#> # A tibble: 16 × 2
#> f n
#> <fct> <int>
#> 1 No answer 183
#> 2 Don't know 267
#> 3 Refused 975
#> 4 $25000 or more 7363
#> 5 $20000 - 24999 1283
#> 6 $15000 - 19999 1048
#> 7 $10000 - 14999 1168
#> 8 $8000 to 9999 340
#> 9 $7000 to 7999 188
#> 10 $6000 to 6999 215
#> 11 $5000 to 5999 227
#> 12 $4000 to 4999 226
#> 13 $3000 to 3999 276
#> 14 $1000 to 2999 395
#> 15 Lt $1000 286
#> 16 Not applicable 7043
convert_income(levels(gss_cat$rincome))
#> [1] "No answer" "Don't know" "Refused" "Gt $25000"
#> [5] "Gt $20000" "Gt $15000" "Gt $10000" "Gt $5000"
#> [9] "Gt $5000" "Gt $5000" "Gt $5000" "Gt $0"
#> [13] "Gt $0" "Gt $0" "Gt $0" "Not applicable"
rincome2 <- fct_relabel(gss_cat$rincome, convert_income)
fct_count(rincome2)
#> # A tibble: 10 × 2
#> f n
#> <fct> <int>
#> 1 No answer 183
#> 2 Don't know 267
#> 3 Refused 975
#> 4 Gt $25000 7363
#> 5 Gt $20000 1283
#> 6 Gt $15000 1048
#> 7 Gt $10000 1168
#> 8 Gt $5000 970
#> 9 Gt $0 1183
#> 10 Not applicable 7043
相關用法
- R forcats fct_relevel 手動重新排序因子級別
- 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等大神的英文原創作品 Relabel factor levels with a function, collapsing as necessary。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。