How to select columns from a tibble using a pattern matching column names

Let's say I have a tibble named sample_tbl with thousands of columns. What I want to do is this:

sample_tbl %<>% .[, str_detect(colnames(.), "some_pattern_here")]

But in my opinion using [ and ] for selecting columns makes codes less readable. Is there any alternative way to do this specific job? As far as I know select does not support pattern matching for column names.

Have you tried something like this?

sample_tbl %<>% select(matches("some_pattern_here"))
4 Likes

Oh I didn't know there existed this kind of functions. This is exactly what I wanted! Thanks!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.