spindoctor
spindoctor

Reputation: 1897

Can I use Boolean operators with R tidy select functions

is there a way I can use Boolean operators (e.g. | or &) with the tidyselect helper functions to select variables?

The code below illustrates what currently works and what, in my mind, should work but doesn't.

df<-sample(seq(1,4,1), replace=T, size=400)
df<-data.frame(matrix(df, ncol=10))

#make variable names
library(tidyverse)
library(stringr)
vars1<-str_c('q1_', seq(1,5,1))
vars2<-str_c('q9_', seq(1,5,1))
#Assign
names(df)<-c(vars1, vars2)
names(df)


#This works 
df %>% 
  select(starts_with('q1_'), starts_with('q9'))
#This does not work using |
df %>% 
  select(starts_with('q1_'| 'q9_'))
#This does not work with c() 
df %>% 
  select(starts_with(c('q1_', 'q9_')))

Upvotes: 2

Views: 310

Answers (2)

Gregor Thomas
Gregor Thomas

Reputation: 146070

You can use multiple starts_with, e.g.,

df %>% select(starts_with('q1_'), starts_with('q9_'))

You can use | in a regular expression and matches() (in this case, in combination with ^, the regex beginning-of-string)

df %>% select(matches('^q1_|^q9_'))

Upvotes: 4

tmfmnk
tmfmnk

Reputation: 40171

You can also approach it using purrr:

map(.x = c("q1_", "q9_"), ~ df %>%
     select(starts_with(.x))) %>%
 bind_cols()

   q1_1 q1_2 q1_3 q1_4 q1_5 q9_1 q9_2 q9_3 q9_4 q9_5
1     2    4    3    1    2    2    3    1    1    3
2     1    3    3    4    4    3    2    2    1    3
3     2    2    3    4    3    4    1    3    2    4
4     1    2    4    2    4    3    3    1    3    3
5     3    1    2    3    3    2    2    3    3    3
6     4    2    3    4    1    4    2    4    2    4
7     3    1    4    1    4    2    4    4    1    2
8     2    2    3    2    1    3    3    3    1    4
9     1    4    2    3    4    4    1    1    3    4
10    1    1    2    4    1    1    4    4    1    2

Upvotes: 0

Related Questions