简体   繁体   English

使用 dplyr::filter() 删除 NA 观察

[英]Removing NA observations with dplyr::filter()

My data looks like this:我的数据如下所示:

library(tidyverse)

df <- tribble(
    ~a, ~b, ~c,
    1, 2, 3, 
    1, NA, 3, 
    NA, 2, 3
)

I can remove all NA observations with drop_na() :我可以使用drop_na()删除所有NA观察:

df %>% drop_na()

Or remove all NA observations in a single column ( a for example):或删除所有NA观测在单个列( a为例):

df %>% drop_na(a)

Why can't I just use a regular != filter pipe?为什么我不能只使用普通的!=过滤器管道?

df %>% filter(a != NA)

Why do we have to use a special function from tidyr to remove NAs?为什么我们必须使用 tidyr 的特殊函数来删除 NAs?

For example:例如:

you can use:您可以使用:

df %>% filter(!is.na(a))

to remove the NA in column a.删除 a 列中的 NA。

From @Ben Bolker:来自@Ben Bolker:

[T]his has nothing specifically to do with dplyr::filter() [T]他与 dplyr::filter() 没有特别的关系

From @Marat Talipov:来自@Marat Talipov:

[A]ny comparison with NA, including NA==NA, will return NA [A]任何与NA的比较,包括NA==NA,都会返回NA

From a related answer by @farnsy:来自@farnsy 的相关回答

The == operator does not treat NA's as you would expect it to. == 运算符不会像您期望的那样处理 NA。

Think of NA as meaning "I don't know what's there".将 NA 视为“我不知道那里有什么”的意思。 The correct answer to 3 > NA is obviously NA because we don't know if the missing value is larger than 3 or not. 3 > NA 的正确答案显然是 NA,因为我们不知道缺失值是否大于 3。 Well, it's the same for NA == NA.嗯,对于 NA == NA 也是一样。 They are both missing values but the true values could be quite different, so the correct answer is "I don't know."它们都是缺失值,但真实值可能大不相同,因此正确答案是“我不知道”。

R doesn't know what you are doing in your analysis, so instead of potentially introducing bugs that would later end up being published an embarrassing you, it doesn't allow comparison operators to think NA is a value. R 不知道您在分析中正在做什么,因此与其潜在地引入后来最终会发布让您尴尬的错误,不如让比较运算符认为 NA 是一个值。

如果 2020 年有人在这里,在制作完所有管道后,如果 u pipe %>% na.exclude将带走管道中的所有 NA!

I always use this and it is working perfectly我总是使用它,它运行良好

cool$day[cool$day==''] <- NA cool$day[cool$day==''] <- NA
cool$day[is.na(cool$day)] <- "NA" cool$day[is.na(cool$day)] <- "NA"

cool <- cool[!cool$day == "NA", ]酷 <- 酷[!cool$day == "NA", ]

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

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