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


R forcats fct_drop 刪除未使用的級別

base::droplevels() 相比,不會刪除具有值的 NA 級別。

用法

fct_drop(f, only = NULL)

參數

f

因子(或字符向量)。

only

限製要刪除的級別集的字符向量。如果提供,則隻有沒有條目且出現在該向量中的級別才會被刪除。

也可以看看

fct_expand() 為因子添加額外級別。

例子

f <- factor(c("a", "b"), levels = c("a", "b", "c"))
f
#> [1] a b
#> Levels: a b c
fct_drop(f)
#> [1] a b
#> Levels: a b

# Set only to restrict which levels to drop
fct_drop(f, only = "a")
#> [1] a b
#> Levels: a b c
fct_drop(f, only = "c")
#> [1] a b
#> Levels: a b
源代碼:R/drop.R

相關用法


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