1

Looking for help to find a way to pass a vector of strings into a select statement. I want to subset a data frame to only output variables that contain the same string as my vector. I don't want it to match exactly and hence need to pass a function like contains as there are some text in the data frame variables that I do not have in my vector.

here is an example of the vector I want to pass into my select statement.

c("clrs_name", "_clrs_sitedetails_value", "_clrs_targetlicence_value", 
"clrs_licenceclass", "clrs_licenceownership", "clrs_type", "statuscode")

For example, I want to extract the variable "odate_value_clrs_name" from my data frame and the string "clrs_name" in vector should extract that, but I am not sure how to incorporate contains and a vector into a select statement.

SteveM
  • 213
  • 3
  • 13
  • It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick Feb 21 '20 at 19:52
  • Though it's unclear if you are trying to match column names or column values. If column names then maybe a dup like this is more appropriate: https://stackoverflow.com/questions/49460542/dplyr-select-based-on-multiple-strings-in-a-column – MrFlick Feb 21 '20 at 19:55
  • 2
    You can `paste` with `collapse="|"` and pass it in `matches` in `select` i.e. `library(dplyr);df1 %>% select(matches(str_c(v1, collapse = "|")))` – akrun Feb 21 '20 at 19:58
  • `matches` would not throw an error even if some elements are missing – akrun Feb 21 '20 at 20:01
  • 1
    @akrun - thanks that worked – SteveM Feb 21 '20 at 20:08

1 Answers1

2

We can use matches in select after collapseing the pattern vector with | by either paste from base R or str_c (str_c would also return NA if there are any NAs). This would not return any error or warning if one of the pattern is missing or doesn't have any match with the column names

library(dplyr)
library(stringr)
df1 %>%
     select(matches(str_c(v1, collapse = "|")))

where

v1 <- c("clrs_name", "_clrs_sitedetails_value", "_clrs_targetlicence_value", 
  "clrs_licenceclass", "clrs_licenceownership", "clrs_type", "statuscode")
akrun
  • 874,273
  • 37
  • 540
  • 662