简体   繁体   English

将 NA 转换为因子水平

[英]Convert NA into a factor level

I have a vector with NA values that I would like to replace by a new factor level NA .我有一个带有NA值的向量,我想用新的因子级别NA替换它。

a = as.factor(as.character(c(1, 1, 2, 2, 3, NA)))
a
[1] 1    1    2    2    3    <NA>
Levels: 1 2 3

This works, but it seems like a strange way to do it.这有效,但似乎是一种奇怪的方法。

a = as.factor(ifelse(is.na(a), "NA", a))
class(a)
[1] "factor"

This is the expected output:这是预期的输出:

a
[1] 1  1  2  2  3  NA
Levels: 1 2 3 NA

You can use addNA() .您可以使用addNA()

x <- c(1, 1, 2, 2, 3, NA)
addNA(x)
# [1] 1    1    2    2    3    <NA>
# Levels: 1 2 3 <NA>

This is basically a convenience function for factoring with exclude = NULL .这基本上是一个使用exclude = NULL进行分解的便利函数。 From help(factor) -help(factor) -

addNA modifies a factor by turning NA into an extra level (so that NA values are counted in tables, for instance). addNA通过将NA转换为额外级别addNA修改一个因子(例如,这样NA值会被计入表格中)。

So another reason this is nice is because if you already have a factor f , you can use addNA() to quickly add NA as a factor level without changing f .所以这很好的另一个原因是,如果你已经有了一个因子f ,你可以使用addNA()快速添加NA作为因子水平而不改变f As mentioned in the documentation, this is handy for tables.正如文档中提到的,这对表格很方便。 It also reads nicely.它也很好读。

You can add the NA as a level and change the level name to something more explicit than <NA> using fct_explicit_na from package forcats .您可以添加NA的水平,并更改级别名称的东西比更明确的<NA>使用fct_explicit_na从包forcats。

library(forcats)

By default you get the new level as (Missing) :默认情况下,您获得的新级别为(Missing)

fct_explicit_na(a)

[1] 1         1         2         2         3         (Missing)
Levels: 1 2 3 (Missing)

You can set it to something else:您可以将其设置为其他内容:

fct_explicit_na(a, "unknown")

[1] 1       1       2       2       3       unknown
Levels: 1 2 3 unknown

Set the exclude argument to NULL to include NAs as levels (and use factor instead of as.factor. Does the same thing and has more arguments to set):将 exclude 参数设置为 NULL 以包含 NA 作为级别(并使用 factor 而不是 as.factor。做同样的事情并有更多的参数要设置):

a = factor(as.character(c(1, 1, 2, 2, 3, NA)), exclude = NULL)

> a
[1] 1    1    2    2    3    <NA>
Levels: 1 2 3 <NA>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM