r,不等于,不包含nas [英] r, does not equal, nas are not included

查看:83
本文介绍了r,不等于,不包含nas的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试过滤我的数据以忽略某些值。问题是我要包括NA。当我使用不等于!=的运算符时,NA也将被删除。

I'm trying to filter my data to leave out certain values. The problem is that I want to include the NAs. When I use the does not equal, "!=", operator, the NAs are also removed.

    a= c("A","C","B",NA,"C","A",NA,"B","A")
    df = data.frame(a)

    df2 <- df %>%
       filter(a != "B")
    df2

例如,我希望df2包含不等于B(A和C)和NA的所有内容,而不是仅包含A和C。

For example, I would like df2 to include everything not equal to B (A and C) and NA instead of only A and C.

推荐答案

我们可以在过滤器函数中包含另一个条件,该条件将保留 NA 值:

We can include another condition in the filter function which will keep the NA values:

df %>%
   filter(a != "B" | is.na(a))

#      a
# 1    A
# 2    C
# 3 <NA>
# 4    C
# 5    A
# 6 <NA>
# 7    A

来自 ?NA


逻辑计算将 NA 视为缺少的 TRUE / FALSE值...

Logical computations treat NA as a missing `TRUE/FALSE value...

还有更多解释,但您可以查阅帮助文件。

There's more to the explanation, but you can consult the help file.

这篇关于r,不等于,不包含nas的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆