title: How to address column names and column contents of R dataframe using variables?
body: <p>I have the following dataframe created as:</p>
  <p><code>A &lt;- data.frame(Col1 = c(&quot;Item1&quot;,&quot;Item1&quot;,&quot;Item1&quot;,&quot;Item2&quot;,&quot;Item2&quot;,&quot;Item2&quot;,&quot;Item1&quot;,&quot;Item1&quot;,&quot;Item2&quot;,&quot;Item2&quot;), Col2 = 101:110)</code></p>
  <p><a href="https://i.stack.imgur.com/ceVAe.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ceVAe.png" alt="enter image description here" /></a></p>
  <p>Now, I need to create a new dataframe with the condition that it should only include all items of the <em>dataframe A</em> where the value of <em>Col1 = &quot;Item2&quot;</em>. That is, I need to get the following:</p>
  <p><a href="https://i.stack.imgur.com/zBU2O.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/zBU2O.png" alt="enter image description here" /></a></p>
  <p>Traditionally, I can do this simply as follows:</p>
  <pre><code>A_new &lt;- A[A$Col1==&quot;Item2&quot;,]
  </code></pre>
  <p>(OR)</p>
  <pre><code>A_new &lt;- A['$'(A,Col1)==&quot;Item2&quot;,]
  </code></pre>
  <p>But rather, I need to do do this via variable names instead, owing to the nature of my actual code.
  So I define two variables as follows:</p>
  <pre><code>var1 &lt;- &quot;Col1&quot;
  var2 &lt;- &quot;Item2&quot;
  </code></pre>
  <p>And now when I try to create this new dataframe using the following, I simply get an empty dataframe with the two columns and no rows:</p>
  <pre><code>A_new &lt;- A['$'(A,var1)==var2,]
  </code></pre>
  <p>And also, when I apply this exact same logic on my actual code trying to create the subset dataframe, I additionally get an error saying my <em>var1</em> is <code>invalid subscript type 'language'</code></p>
  <p><em><strong>Can anyone help me out with creating this subset dataframe by addressing column names and column contents using variables?</strong></em></p>