0

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
Dambo
  • 3,318
  • 5
  • 30
  • 79
  • A discussion of NA==NA is here... http://stackoverflow.com/questions/25100974/na-matches-na-but-is-not-equal-to-na-why – cory Aug 05 '16 at 15:00

2 Answers2

1

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

akrun
  • 874,273
  • 37
  • 540
  • 662
1

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.

Dason
  • 60,663
  • 9
  • 131
  • 148