ben_says
ben_says

Reputation: 2513

Passing NSE to dplyr's filter

library(dplyr)

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

i=1

setup is complete, this works...

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

why does the nse fail on adding the filter?

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

Upvotes: 0

Views: 107

Answers (1)

akrun
akrun

Reputation: 887108

We may need the 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

Upvotes: 1

Related Questions