当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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"。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。