2017-07-08 1 views
3

遅延評価とdplyrを使用して、愚かな問題を抱えています。 NAをフィルタリングしようとしていますが、なぜlazyevalのバージョンが機能しないのか分かりません。おそらく私は何かを見逃しているが、私はそれを見つけることができない。そうか、それともバグですか?エラーをスローすることなく実行Lazy eval、dplyr "filter"、NAs

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")) 

両コード:ここ

最小再現一例です。

+0

dplyrのバージョンは何を? –

+0

あまり古い、0.5.0。最新バージョンをインストールして試してみてください。 – Elijah

答えて

2

名前として"t"を渡す必要があります。

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

あなたのコードは現状では、あなたは毎回FALSEであるis.na("t")を作るためにis.na()"t"を挿入しています。そしてそれを否定することは、毎回TRUEを与え、したがってすべての行を与えます。

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

ありがとう!私は何かを忘れていることを知っていた! – Elijah

2

dplyrは、新しい構文の賛成で*_機能を卑下、(hereを文書化)lazyevalからrlangにそのNSEシステム上で切り替えた:

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