如何使用dplyr过滤数据而不会丢失NA行 [英] How to filter data without losing NA rows using dplyr
问题描述
该子集上方的帖子使用逻辑索引。 在dplyr中有办法吗?
The post above subsets using logical indexing. Is there a way to do it in dplyr?
此外, dplyr何时自动删除NA?经验,当我过滤掉一个特定的字符串时,它将删除NA,例如:
Also, when does dplyr automatically delete NAs? In my experience, it removes NA when I filter out a specific string, eg:
b = a %>% filter(col != "str")
我认为这不排除 NA code>值,但确实如此。但是当我使用其他格式的过滤时,它不会自动排除
NA
,例如:
I would think this would not exclude NA
values but it does. But when I use other format of filtering, it does not automatically exclude NA
, eg:
b = a %>% filter(!grepl("str", col))
我想了解过滤器的这一功能。我将不胜感激任何帮助。谢谢!
I would like to understand this feature of filter. I would appreciate any help. Thank you!
推荐答案
dplyr :: filter
的文档说。 ..与基本子集不同,条件条件为NA的行将被删除。
The documentation for dplyr::filter
says... "Unlike base subsetting, rows where the condition evaluates to NA are dropped."
NA!= str
的值为 NA
,因此被过滤器
丢弃。
NA != "str"
evaluates to NA
so is dropped by filter
.
!grepl( str,NA)
返回 TRUE
,因此保留。
如果您希望过滤器
保持 NA
,则可以执行 filter(is.na(col)| col!= str)
If you want filter
to keep NA
, you could do filter(is.na(col)|col!="str")
这篇关于如何使用dplyr过滤数据而不会丢失NA行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!