title: subset columns based on column names
body: <p>I have a df1 with ids</p>
  
  <pre><code>df1 &lt;- read.table(text="ID
  8765
                      1879
                      8706
                      1872
                      0178
                      0268
                      0270
                      0269
                      0061
                      0271", header=T)
  </code></pre>
  
  <p>second df2 with columns names </p>
  
  <pre><code>&gt; names(df2)
   [1] "TW_3784.IT"   "TW_3970.IT"   "TW_1879.IT"   "TW_0178.IT"   "SF_0271.IT" "TW_3782.IT"  
   [7] "TW_3783.IT"   "TW_8765.IT"   "TW_8706.IT"   "SF_0268.IT" "SF_0270.IT" "SF_0269.IT"
  [13] "SF_0061.IT"
  </code></pre>
  
  <p>What i need is to keep only columns from df2 that partial match with df1</p>
  
  <h1>code</h1>
  
  <h3>using dplyr</h3>
  
  <pre><code>df3 = df2 %&gt;% 
    dplyr::select(df2 , dplyr::contains(df1$ID))
  error
  
  Error in dplyr::contains(df1$ID) : is_string(match) is not TRUE
  </code></pre>
  
  <h3>using grepl</h3>
  
  <pre><code>df3 = df2[,grepl(df1$ID, names(df2))]
  
  error
  In grepl(df1$ID, names(df2)) :
    argument 'pattern' has length &gt; 1 and only the first element will be used
  </code></pre>