使用 dplyr::filter() 删除 NA 观察 [英] Removing NA observations with dplyr::filter()

查看:22
本文介绍了使用 dplyr::filter() 删除 NA 观察的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的数据如下所示:

library(tidyverse)

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

我可以使用 drop_na() 删除所有 NA 观察:

I can remove all NA observations with drop_na():

df %>% drop_na()

或者删除单个列中的所有 NA 观察(例如 a):

Or remove all NA observations in a single column (a for example):

df %>% drop_na(a)

为什么我不能只使用常规的 != 过滤器管道?

Why can't I just use a regular != filter pipe?

df %>% filter(a != NA)

为什么我们必须使用 tidyr 中的特殊函数来删除 NAs?

Why do we have to use a special function from tidyr to remove NAs?

推荐答案

来自@Ben Bolker:

From @Ben Bolker:

[T]他与 dplyr::filter() 没有特别的关系

[T]his has nothing specifically to do with dplyr::filter()

来自@Marat Talipov:

From @Marat Talipov:

[A]任何与NA的比较,包括NA==NA,都会返回NA

[A]ny comparison with NA, including NA==NA, will return NA

来自相关答案@farnsy:

== 运算符不会像您期望的那样处理 NA.

The == operator does not treat NA's as you would expect it to.

将 NA 视为我不知道那里有什么"的意思.正确答案to 3 > NA 显然是 NA 因为我们不知道缺失值是否大于 3.嗯,对于 NA == NA 也是一样.他们是两个缺失值但真实值可能大不相同,所以正确答案是我不知道."

Think of NA as meaning "I don't know what's there". The correct answer to 3 > NA is obviously NA because we don't know if the missing value is larger than 3 or not. Well, it's the same for 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 不知道您在分析中在做什么,因此而不是可能会引入后来最终会发布的错误让你尴尬,它不允许比较运算符认为 NA是一个值.

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.

这篇关于使用 dplyr::filter() 删除 NA 观察的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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