For a recent project, a dataset’s columns were indicator variables for options selected from drop-down menus. The column names consisted of the menu name suffix-ed with the option number (ex. a___1 is the first option from menu a). The data dictionary only included menu names. I wanted to leverage dplyr’s starts_with() to select columns for multiple menus without hard-coding each starts_with().

Programatically select columns with dplyr’s select helpers

library(tidyverse)

df <- data.frame(
  'a___1' = c(1, 0), 'a___2' = c(0, 1),
  'b___1' = c(1, 0), 'b___2' = c(0, 1),
  'c___1' = c(1, 0), 'c___2' = c(0, 1)
)

df
#>   a___1 a___2 b___1 b___2 c___1 c___2
#> 1     1     0     1     0     1     0
#> 2     0     1     0     1     0     1

vars <- c('a', 'b')
quo_vars <- map(vars, ~quo(starts_with(.)))

df %>% select(!!!quo_vars)
#>   a___1 a___2 b___1 b___2
#> 1     1     0     1     0
#> 2     0     1     0     1

Created on 2018-10-21 by the reprex package (v0.2.1)

Session info
devtools::session_info()
#> Session info -------------------------------------------------------------
#>  setting  value                       
#>  version  R version 3.5.1 (2018-07-02)
#>  system   x86_64, darwin15.6.0        
#>  ui       X11                         
#>  language (EN)                        
#>  collate  en_US.UTF-8                 
#>  tz       America/New_York            
#>  date     2018-10-21
#> Packages -----------------------------------------------------------------
#>  package    * version     date       source                      
#>  assertthat   0.2.0       2017-04-11 CRAN (R 3.5.0)              
#>  backports    1.1.2       2017-12-13 CRAN (R 3.5.0)              
#>  base       * 3.5.1       2018-07-05 local                       
#>  bindr        0.1.1       2018-03-13 CRAN (R 3.5.0)              
#>  bindrcpp     0.2.2       2018-03-29 CRAN (R 3.5.0)              
#>  broom        0.5.0       2018-07-17 CRAN (R 3.5.0)              
#>  cellranger   1.1.0       2016-07-27 CRAN (R 3.5.0)              
#>  cli          1.0.0       2017-11-05 CRAN (R 3.5.0)              
#>  colorspace   1.3-2       2016-12-14 CRAN (R 3.5.0)              
#>  compiler     3.5.1       2018-07-05 local                       
#>  crayon       1.3.4       2017-09-16 CRAN (R 3.5.0)              
#>  datasets   * 3.5.1       2018-07-05 local                       
#>  devtools     1.13.6      2018-06-27 CRAN (R 3.5.0)              
#>  digest       0.6.18      2018-10-10 cran (@0.6.18)              
#>  dplyr      * 0.7.6       2018-06-29 CRAN (R 3.5.1)              
#>  evaluate     0.11        2018-07-17 CRAN (R 3.5.0)              
#>  forcats    * 0.3.0       2018-02-19 CRAN (R 3.5.0)              
#>  ggplot2    * 3.0.0       2018-07-03 CRAN (R 3.5.0)              
#>  glue         1.3.0       2018-07-17 CRAN (R 3.5.0)              
#>  graphics   * 3.5.1       2018-07-05 local                       
#>  grDevices  * 3.5.1       2018-07-05 local                       
#>  grid         3.5.1       2018-07-05 local                       
#>  gtable       0.2.0       2016-02-26 CRAN (R 3.5.0)              
#>  haven        1.1.2       2018-06-27 CRAN (R 3.5.0)              
#>  hms          0.4.2       2018-03-10 CRAN (R 3.5.0)              
#>  htmltools    0.3.6       2017-04-28 CRAN (R 3.5.0)              
#>  httr         1.3.1       2017-08-20 CRAN (R 3.5.0)              
#>  jsonlite     1.5         2017-06-01 CRAN (R 3.5.0)              
#>  knitr        1.20        2018-02-20 CRAN (R 3.5.0)              
#>  lattice      0.20-35     2017-03-25 CRAN (R 3.5.0)              
#>  lazyeval     0.2.1       2017-10-29 CRAN (R 3.5.0)              
#>  lubridate    1.7.4       2018-04-11 CRAN (R 3.5.0)              
#>  magrittr     1.5         2014-11-22 CRAN (R 3.5.0)              
#>  memoise      1.1.0       2017-04-21 CRAN (R 3.5.0)              
#>  methods    * 3.5.1       2018-07-05 local                       
#>  modelr       0.1.2       2018-05-11 CRAN (R 3.5.0)              
#>  munsell      0.5.0       2018-06-12 CRAN (R 3.5.0)              
#>  nlme         3.1-137     2018-04-07 CRAN (R 3.5.0)              
#>  pillar       1.3.0       2018-07-14 CRAN (R 3.5.0)              
#>  pkgconfig    2.0.1       2017-03-21 CRAN (R 3.5.0)              
#>  plyr         1.8.4       2016-06-08 CRAN (R 3.5.0)              
#>  purrr      * 0.2.5       2018-05-29 CRAN (R 3.5.0)              
#>  R6           2.2.2       2017-06-17 CRAN (R 3.5.0)              
#>  Rcpp         0.12.18     2018-07-23 CRAN (R 3.5.0)              
#>  readr      * 1.1.1       2017-05-16 CRAN (R 3.5.0)              
#>  readxl       1.1.0       2018-04-20 CRAN (R 3.5.0)              
#>  rlang        0.2.99.0000 2018-10-14 Github (r-lib/rlang@30d6671)
#>  rmarkdown    1.10        2018-06-11 CRAN (R 3.5.0)              
#>  rprojroot    1.3-2       2018-01-03 CRAN (R 3.5.0)              
#>  rvest        0.3.2       2016-06-17 CRAN (R 3.5.0)              
#>  scales       1.0.0       2018-08-09 CRAN (R 3.5.0)              
#>  stats      * 3.5.1       2018-07-05 local                       
#>  stringi      1.2.4       2018-07-20 CRAN (R 3.5.0)              
#>  stringr    * 1.3.1       2018-05-10 CRAN (R 3.5.0)              
#>  tibble     * 1.4.2       2018-01-22 CRAN (R 3.5.0)              
#>  tidyr      * 0.8.1       2018-05-18 CRAN (R 3.5.0)              
#>  tidyselect   0.2.4       2018-02-26 CRAN (R 3.5.0)              
#>  tidyverse  * 1.2.1       2017-11-14 CRAN (R 3.5.0)              
#>  tools        3.5.1       2018-07-05 local                       
#>  utils      * 3.5.1       2018-07-05 local                       
#>  withr        2.1.2       2018-03-15 CRAN (R 3.5.0)              
#>  xml2         1.2.0       2018-01-24 CRAN (R 3.5.0)              
#>  yaml         2.2.0       2018-07-25 CRAN (R 3.5.0)