2

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.

5
  • It's easier to help you if you include a simple reproducible example with sample input and desired output that can be used to test and verify possible solutions. Commented Feb 21, 2020 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: stackoverflow.com/questions/49460542/… Commented Feb 21, 2020 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 = "|"))) Commented Feb 21, 2020 at 19:58
  • matches would not throw an error even if some elements are missing Commented Feb 21, 2020 at 20:01
  • 1
    @akrun - thanks that worked Commented Feb 21, 2020 at 20:08

1 Answer 1

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") 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.