简体   繁体   English

为什么 dplyr 过滤器不捕获 NA

[英]why is dplyr filter not capturing NA's

I have the following data frame我有以下数据框

  FileNumber ReferralDate Status
1  510709784   2018-10-07 CLOSED
2         NA         <NA>   <NA>
3  510704781   2018-05-04 CLOSED
4         NA         <NA>   <NA>
5         NA         <NA>   <NA>
6         NA         <NA>   <NA>

This is the structure of the data frame这是数据框的结构

'data.frame':   6 obs. of  3 variables:
 $ FileNumber  : int  510709784 NA 510704781 NA NA NA
 $ ReferralDate: chr  "2018-10-07" NA "2018-05-04" NA ...
 $ Status      : chr  "CLOSED" NA "CLOSED" NA ...

when I try to capture the NA values in either the FileNumber column or the Status column using the following code.当我尝试使用以下代码捕获 FileNumber 列或 Status 列中的NA值时。 But it doesn't seem to work.但它似乎不起作用。 Why is this happening为什么会发生这种情况

  > df%>%filter(Status=="<NA>")
[1] FileNumber   ReferralDate Status      
<0 rows> (or 0-length row.names)
> df%>%mutate(Status=as.factor(Status))%>%filter(Status=="<NA>")
[1] FileNumber   ReferralDate Status      
<0 rows> (or 0-length row.names)
> df%>%filter(FileNumber=="NA")
[1] FileNumber   ReferralDate Status      
<0 rows> (or 0-length row.names)
library(dplyr)

df <- data.frame(FileNumber = c(510709784, NA, 510704781, NA, NA, NA),
                 ReferralDate = c("2018-10-07", NA, "2018-05-04", NA, NA, NA),
                 Status = c("CLOSED", NA, "CLOSED", NA, NA, NA),
                 stringsAsFactors = FALSE)

Use is.na() to refer to NA , not ==使用is.na()来引用NA ,而不是==

df %>% filter(is.na(Status))
  FileNumber ReferralDate Status
1         NA         <NA>   <NA>
2         NA         <NA>   <NA>
3         NA         <NA>   <NA>
4         NA         <NA>   <NA>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM