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

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

问题描述

我的资料如下所示:

library(tidyverse)

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

我可以删除所有 NA 观察与 drop_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:


他没有与dplyr :: filter()

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

从@Marat Talipov:

From @Marat Talipov:


[NA]与NA的比较,包括NA == NA,将返回NA

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

相关答案 by @ farnsy:

From a related answer by @farnsy:


==操作符不会像你所期望的那样对待NA。

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

认为NA意思是我不知道那里是什么。正确答案
到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天全站免登陆