Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

faq-selection-context

FAQ - Error: Must be used within a selecting function


Description

Functions like starts_with(), contains() or matches() are selection helpers that only work in a selection context.

Examples of valid selection contexts are:

  • Inside dplyr::select().

  • The cols argument of tidyr::pivot_longer().

Using a selection helper anywhere else results in an error:

starts_with("foo")
#> Error: `starts_with()` must be used within a *selecting* function.
#> i See <https://tidyselect.r-lib.org/reference/faq-selection-context.html>.

mtcars[contains("foo")]
#> Error: `contains()` must be used within a *selecting* function.
#> i See <https://tidyselect.r-lib.org/reference/faq-selection-context.html>.

subset(mtcars, select = matches("foo"))
#> Error: `matches()` must be used within a *selecting* function.
#> i See <https://tidyselect.r-lib.org/reference/faq-selection-context.html>.

If you see this error, you’ve probably used a selection helper in the wrong place, possibly as the result of a typo (e.g. misplaced comma or wrong argument name).


tidyselect

Select from a Set of Strings

v1.1.1
MIT + file LICENSE
Authors
Lionel Henry [aut, cre], Hadley Wickham [aut], RStudio [cph]
Initial release

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.