Using select_ and starts_with R

Why is this code not working?

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

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

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

0
source share
1 answer

The difference between select()and select_()is their non-standard / standard evaluation of the argument. If a select_()function of type is used as an argument starts_with(), it should be indicated by a tilde:

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

This gives the same result as normal use select:

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

For more information, see a vignette of a non-standard evaluation.: vignette("nse").

+2
source

Source: https://habr.com/ru/post/1682436/


All Articles