Search code examples
rselectcontains

Passing a vector through a select statement


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.


Solution

  • 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")