title: Use a character vector to extract columns from a dataframe in R
body: <p>I have the following character vector</p>
  <pre><code>Group1
  [1] &quot;c(\&quot;Ca\&quot;, \&quot;Mg\&quot;, \&quot;SO4\&quot;, \&quot;HCO3\&quot;, \&quot;NO3\&quot;, \&quot;Cr\&quot;, \&quot;Ni\&quot;, \&quot;Sr\&quot;)&quot;
  &gt; class(Group1)
  [1] &quot;character&quot;
  </code></pre>
  <p>and I want το use the elements of Group1 (e.g. &quot;Ca&quot;) to extract columns from a dataframe (df) based on colnames() function.</p>
  <pre><code>colnames(df) &lt;- Group1
  </code></pre>
  <p>I have tried it but it did not work. My guess is that I need to remove the &quot;&quot; symbol.</p>