r dplyr ends_with multiple string matches

From version 1.0.0, you can combine multiple selections using Boolean logic such as ! (negate), & (and) and | (or).

### Install development version on GitHub first until CRAN version is available
# install.packages("devtools")
# devtools::install_github("tidyverse/dplyr")
library(dplyr, warn.conflicts = FALSE)

df <- data.frame(a10 = 1:4,
                 a11 = 5:8,
                 a20 = 1:4,
                 a12 = 5:8)

df %>% 
  select(ends_with("1") | ends_with("2"))
#>   a11 a12
#> 1   5   5
#> 2   6   6
#> 3   7   7
#> 4   8   8

or use num_range() to select the desired columns

df %>% 
  select(num_range(prefix = "a", range = 11:12))
#>   a11 a12
#> 1   5   5
#> 2   6   6
#> 3   7   7
#> 4   8   8

Created on 2020-02-17 by the reprex package (v0.3.0)


You can also do this using regular expressions. I know you did not want to use matches initially, but it actually works quite well if you use the "end of string" symbol $. Separate your various endings with |.

df <- data.frame(a10 = 1:4,
                 a11 = 5:8,
                 a20 = 1:4,
                 a12 = 5:8)

df %>% select(matches('1$|2$'))
  a11 a12
1   5   5
2   6   6
3   7   7
4   8   8

If you have a more complex example with a long list, use paste0 with collapse = '|'.

dff <- data.frame(a11 = 1:3,
                  a12 = 2:4,
                  a13 = 3:5,
                  a16 = 5:7,
                  my_cat = LETTERS[1:3],
                  my_dog = LETTERS[5:7],
                  my_snake = LETTERS[9:11])

my_cols <- paste0(c(1,2,6,'dog','cat'), 
                  '$', 
                  collapse = '|')

dff %>% select(matches(my_cols))

  a11 a12 a16 my_cat my_dog
1   1   2   5      A      E
2   2   3   6      B      F
3   3   4   7      C      G