Lazy eval, dplyr "filter" and NAs
I have some silly problem using lazy evaluation and dplyr. I'm trying to filter out some NA
and don't know why the lazyeval version is not working. I guess I missed something, but I can't find it. Is this true, or is this a bug?
Here's a minimal 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 an error.
+3
source to share
2 answers
dplyr switched its NSE system from lazyeval to rlang (documented here ), deprecating functions *_
in favor of the new syntax:
library(dplyr) data(iris) iris <- iris %>% mutate(t = c(1, rep(NA, 149))) # normal NSE iris %>% filter(!is.na(t)) #> Sepal.Length Sepal.Width Petal.Length Petal.Width Species t #> 1 5.1 3.5 1.4 0.2 setosa 1 # string-based SE; use `rlang::sym` to convert to quosure and !! to unquote x <- "t" iris %>% filter(!is.na(!!rlang::sym(x))) #> Sepal.Length Sepal.Width Petal.Length Petal.Width Species t #> 1 5.1 3.5 1.4 0.2 setosa 1 # program your own NSE with `quo` and friends x <- quo(t) iris %>% filter(!is.na(!!x)) #> Sepal.Length Sepal.Width Petal.Length Petal.Width Species t #> 1 5.1 3.5 1.4 0.2 setosa 1 # both versions work across the tidyverse iris %>% tidyr::drop_na(!!x) #> Sepal.Length Sepal.Width Petal.Length Petal.Width Species t #> 1 5.1 3.5 1.4 0.2 setosa 1 # though tidyr::drop_na is happy with strings anyway iris %>% tidyr::drop_na("t") #> Sepal.Length Sepal.Width Petal.Length Petal.Width Species t #> 1 5.1 3.5 1.4 0.2 setosa 1
+2
source to share