rdplyrnalazyeval

Lazy eval, dplyr "filter" and NAs


I am having some silly problem using lazy evaluation and dplyr. I'm trying to filter some NAs and don't know why the lazyeval version doesn't work. Probably I'm missing something, but I can't find it. Is that so, or is it a bug?

Here is a minimum reproducible example:

library(dplyr)
library(lazyeval)

data(iris)
iris$t <- c(1:140, rep(NA, 10))

#This Works
temp <- filter(iris, !is.na(t))

#This doesn't
temp <- filter_(iris, interp(~(!is.na(x)), x="t"))

Both codes run without throwing out an error.


Solution

  • You need to pass "t" as a name.

    interp(~(!is.na(x)), x = as.name("t"))
    # ~!is.na(t)
    

    As your code stands, you are inserting "t" into is.na() to make is.na("t"), which is FALSE every time. And negating that gives TRUE every time, hence all rows.

    interp(~(!is.na(x)), x = "t")
    # ~!is.na("t")