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.
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))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With