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


R forcats fct_other 手動將級別替換為“其他”


手動將級別替換為"other"

用法

fct_other(f, keep, drop, other_level = "Other")

參數

f

因子(或字符向量)。

keep, drop

選擇 keepdrop 之一:

  • keep 將保留列出的級別,並用 other_level 替換所有其他級別。

  • drop 將用 other_level 替換列出的級別,並保持原樣。

other_level

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

也可以看看

fct_lump() 自動將最稀有(或最常見)的級別轉換為"other"。

例子

x <- factor(rep(LETTERS[1:9], times = c(40, 10, 5, 27, 1, 1, 1, 1, 1)))

fct_other(x, keep = c("A", "B"))
#>  [1] A     A     A     A     A     A     A     A     A     A     A    
#> [12] A     A     A     A     A     A     A     A     A     A     A    
#> [23] A     A     A     A     A     A     A     A     A     A     A    
#> [34] A     A     A     A     A     A     A     B     B     B     B    
#> [45] B     B     B     B     B     B     Other Other Other Other Other
#> [56] Other Other Other Other Other Other Other Other Other Other Other
#> [67] Other Other Other Other Other Other Other Other Other Other Other
#> [78] Other Other Other Other Other Other Other Other Other Other
#> Levels: A B Other
fct_other(x, drop = c("A", "B"))
#>  [1] Other Other Other Other Other Other Other Other Other Other Other
#> [12] Other Other Other Other Other Other Other Other Other Other Other
#> [23] Other Other Other Other Other Other Other Other Other Other Other
#> [34] Other Other Other Other Other Other Other Other Other Other Other
#> [45] Other Other Other Other Other Other C     C     C     C     C    
#> [56] D     D     D     D     D     D     D     D     D     D     D    
#> [67] D     D     D     D     D     D     D     D     D     D     D    
#> [78] D     D     D     D     D     E     F     G     H     I    
#> Levels: C D E F G H I Other
源代碼:R/other.R

相關用法


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