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

编程入门 行业动态 更新时间:2024-10-25 04:16:48
本文介绍了使用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观察

本文发布于:2023-11-22 08:22:17,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1616671.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:dplyr   filter   NA

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!