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

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

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

发布评论

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

>www.elefans.com

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