title: How to search for string within variable labels of a data frame, and return a vector with all these variables in R
body: <p>I have a data frame with a large amount of math and science related items, and I want all math related variables removed. </p>
  
  <p>Variable names has no consistent naming for neither math nor science, so it's hard to search and select based variable name. However, the variable labels are descriptive of what the variable represents. I essentially want all variables with labels that contain the word "math" removed. I tried the following code:</p>
  
  <pre><code>library(dplyr)
  library(Hmisc) 
  
  # Sample data frame:
  M &lt;- c(1, 2)
  S &lt;- c(3, 4)
  old_df &lt;- data.frame(M, S)
  label(old_df$M) &lt;- "My Mathematics Variable"
  label(old_df$S) &lt;- "My Science Variable"
  
  #dplyr syntax:
  new_df &lt;- old_df %&gt;% select( -contains(hmisc::label(.) == "MATH" ) )
  </code></pre>
  
  <p>using the <code>Hmisc::label()</code>-function to retrieve a vector with labels. </p>
  
  <p>Sample code of the <code>label()</code>-function:</p>
  
  <pre><code>&gt; label(old_df)
                          M                         S 
  "My Mathematics Variable"     "My Science Variable" 
  &gt; str(label(old_df))
   Named chr [1:2] "My Mathematics Variable" "My Science Variable"
   - attr(*, "names")= chr [1:2] "M" "S"
  </code></pre>
  
  <p>I need a what to search through the label items and find the string "math" within. I tried coerce to a matrix and data frame, but I still can't figure out how to search and retrive the variable names. Any suggestions that will get this to work is welcome.</p>