0
library(dplyr)
dt <- data.frame(A=c(1,2), B=c(NA,5))
tbl_df(dt) %>% filter(A!=B)

I could get it that it can't compare > pr <. But it's kinda more intuitive to think NA is not equal to other values except other NA. Is there a work around to this?

user2941942
  • 85
  • 1
  • 8
  • I guess the `filter` by default removes the NA rows. One option might be `tbl_df(dt) %>% filter(A!=B| is.na(B))` – akrun May 26 '16 at 03:01
  • 1
    Because `1 != NA` returns `NA`, not `TRUE`. To get a comparison with `NA` to return `TRUE`, you need `is.na`. – alistaire May 26 '16 at 03:05
  • Kind of related to my old question: http://stackoverflow.com/q/16221742/ The suggestion there was `tbl_df(dt) %>% filter(!(A %in% B))`. If you think that's adequate (and I do), we can close your question with a link to mine. – Frank May 26 '16 at 04:00

1 Answers1

0

One option would be to create another condition with is.na

tbl_df(dt) %>% 
      filter(A!=B| rowSums(is.na(.))>0)
#      A     B
#  <dbl> <dbl>
#1     1    NA
#2     2     5

If we look at the output from the comparison

with(dt, A!=B)
#[1]   NA TRUE

The first one returns NA as any comparison with NA returns NA.

akrun
  • 874,273
  • 37
  • 540
  • 662