agenis agenis - 4 months ago 22
R Question

select columns based on multiple strings with dplyr

I want to select multiple columns based on their names with a regex expression. I am trying to do it with the piping syntax of the

package. I checked the other topics, but only found answers about a single string.

With base R:

mtcars[grepl('m|ar', names(mtcars))]
### mpg am gear carb
### Mazda RX4 21.0 1 4 4
### Mazda RX4 Wag 21.0 1 4 4

However it doesn't work with the select/contains way:

mtcars %>% select(contains('m|ar'))
### data frame with 0 columns and 32 rows

What's wrong?


You can use matches

 mtcars %>%
        select(matches('m|ar')) %>%
 #              mpg am gear carb
 #Mazda RX4      21  1    4    4
 #Mazda RX4 Wag  21  1    4    4

According to the ?select documentation

‘matches(x, = TRUE)’: selects all variables whose name matches the regular expression ‘x’

Though contains work with a single string

mtcars %>%