Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using starts_with in dplyr with a vector of partial column names

Tags:

r

dplyr

I would like to use dplyr to select certain columns that match to a string vector.

one <- seq(1:10)
two <- rnorm(10)
three <- runif(10, 1, 2)
four <- -10:-1

df <- data.frame(one, two, three, four)

vars <- c('on', 'thr')

I want to select only the columns in df whose titles start with'on' or 'thr':

dplyr::select_(df, starts_with(vars))

However, the above is not working.

like image 601
matsuo_basho Avatar asked Dec 23 '22 20:12

matsuo_basho


1 Answers

The various selection helper functions in dplyr are meant to take only a single character string for matching. You can get around this by combining your strings into one regular expression and using matches:

vars <- paste0("^(", paste(vars, collapse="|"), ")")
select(df, matches(vars))
like image 146
Hong Ooi Avatar answered Apr 30 '23 19:04

Hong Ooi