将NSE传递给dplyr的过滤器

时间:2016-08-31 19:37:26

标签: r dplyr nse

library(dplyr)

specials <- names(mtcars)[1:2]
specials[1]

i=1

设置完成, 这有效......

mtcars %>%
  select_(specials[i], ~gear, ~carb)

为什么nse在添加过滤器时失败?

mtcars %>%
  select_(specials[i], ~gear, ~carb) %>%
  filter_(specials[i] == 21.4)

1 个答案:

答案 0 :(得分:1)

我们可能需要interp

library(lazyeval)
library(dplyr)
mtcars %>%
      select_(specials[i], ~gear, ~carb) %>% 
      filter_(interp(~nm == 21.4, nm = as.name(specials[1])))
#  mpg gear carb
#1 21.4    3    1
#2 21.4    4    2