While I like the answer above, I wanted to give a "tidyverse" solution as well. If you are doing a lot of pipes and trying to do several things at once, as I often do, you may like this answer. Also, I find this code more "humanly" readable.
The function tidyselect::vars_select
will select variables from a character vector in the first argument, which should contain the names of the corresponding data frame, based on a select helper function like starts_with
or matches
library(dplyr)
library(tidyselect)
df <- data.frame(a1 = factor(c("Hi", "Med", "Hi", "Low"),
levels = c("Low", "Med", "Hi"), ordered = TRUE),
a2 = c("A", "D", "A", "C"), a3 = c(8, 3, 9, 9),
b1 = c(1, 1, 1, 2), b2 = c( 5, 4, 3,2), b3 = c(3, 4, 3, 4),
B1 = c(3, 6, 4, 4))
tidyselect::vars_select(names(df), starts_with('b', ignore.case = TRUE))
df %>%
select(vars_select(names(df), starts_with('b', ignore.case = TRUE)))
tidyselect::vars_select(names(df), matches('^[Bb]'))
Note that the default for ignore.case
is TRUE
, but I put it here to show explicitly, and in case future readers are curious how to adjust the code. The include
and exclude
arguments are also very useful. For example, you could use vars_select(names(df), matches('^[Bb]'), include = 'a1')
if you wanted everything that starts with a "B" or a "b", and you wanted to include "a1" as well.