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


R forcats fct_match 測試因子中是否存在水平

lvls 是否出現在 f 中?與 %in% 相比,此函數驗證 lvls 以確保它們確實存在於 f 中。換句話說, x %in% "not present" 將返回 FALSE ,但 fct_match(x, "not present") 將拋出錯誤。

用法

fct_match(f, lvls)

參數

f

因子(或字符向量)。

lvls

指定要查找的級別的字符向量。

邏輯向量

例子

table(fct_match(gss_cat$marital, c("Married", "Divorced")))
#> 
#> FALSE  TRUE 
#>  7983 13500 

# Compare to %in%, misspelled levels throw an error
table(gss_cat$marital %in% c("Maried", "Davorced"))
#> 
#> FALSE 
#> 21483 
if (FALSE) {
table(fct_match(gss_cat$marital, c("Maried", "Davorced")))
}
源代碼:R/match.R

相關用法


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