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