-1

Why doesn't this code work?

mtcars %>% select_("starts_with('d')")

Error in eval(expr, envir, enclos) : could not find function "starts_with"

This is simplified example. I am trying to pass the select_ command to a function.

Joel Kanerva
  • 43
  • 2
  • 8

1 Answers1

4

The difference between select() and select_() is their non-stadard / standard evaluation of the argument. If a function like starts_with() is used as an argument of select_() it should be quoted with a tilde:

library(dplyr)
mtcars %>% select_(~starts_with('d'))

This yields the same output as the normal use of select:

identical(mtcars %>% select_(~starts_with('d')), mtcars %>% select(starts_with('d')))
#[1] TRUE

For more information see the vignette on non-standard evaluation: vignette("nse").

RHertel
  • 23,412
  • 5
  • 38
  • 64
  • t/f: select(arg,...) should be replaced with select_(~arg,...) in every instance that's not on the console? your example seems to contradict this page https://www.rdocumentation.org/packages/dplyr/versions/0.7.3/topics/select – 3pitt Oct 18 '17 at 19:27