Dambo
Dambo

Reputation: 3486

How to retain all NA?

I am trying to filter all NA. I thought this would work but it gives me an empty table rather than what I expected:

data_frame(var=c(sample(letters[1:2],8,replace=8),NA,NA),
           value=rnorm(10,10,2)) %>% 
           filter(var==is.na(var))
#EXPECTED
# var     value
# <chr>     <dbl>
# 1  <NA>  9.119849
# 2  <NA> 13.236334

Upvotes: 0

Views: 66

Answers (2)

Dason
Dason

Reputation: 61913

If var is NA then is.na(var) will be TRUE. So in that case var == is.na(var) is: NA == TRUE. That isn't s true statement so it doesn't pass the filter. I'll let you think about the other cases but I just think your filter isn't doing what you think it is doing.

Upvotes: 1

akrun
akrun

Reputation: 887028

We need just is.na inside filter and not ==

d1 %>% 
   filter(is.na(var))
#  var    value
#  <chr>    <dbl>
#1  <NA>  9.119849
#2  <NA> 13.236334

where 'd1' is the data_frame

Upvotes: 1

Related Questions