當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


R forcats fct_collapse 將因子級別折疊為手動定義的組

將因子級別折疊為手動定義的組

用法

fct_collapse(.f, ..., other_level = NULL, group_other = "DEPRECATED")

參數

.f

因子(或字符向量)。

...

< dynamic-dots > 一係列命名字符向量。每個向量中的級別將替換為名稱。

other_level

用於 "other" 值的級別值。始終放置在關卡末尾。

group_other

已棄用。將 ... 中未命名的所有級別替換為 "Other"?

例子

fct_count(gss_cat$partyid)
#> # 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

partyid2 <- fct_collapse(gss_cat$partyid,
  missing = c("No answer", "Don't know"),
  other = "Other party",
  rep = c("Strong republican", "Not str republican"),
  ind = c("Ind,near rep", "Independent", "Ind,near dem"),
  dem = c("Not str democrat", "Strong democrat")
)
fct_count(partyid2)
#> # A tibble: 5 × 2
#>   f           n
#>   <fct>   <int>
#> 1 missing   155
#> 2 other     393
#> 3 rep      5346
#> 4 ind      8409
#> 5 dem      7180
源代碼:R/collapse.R

相關用法


注:本文由純淨天空篩選整理自Hadley Wickham等大神的英文原創作品 Collapse factor levels into manually defined groups。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。