title: Difference between [] and $ operators for subsetting
body: <p>I am trying to subset a data frame by using a variable name.  I have it working but there is a part which I don't quite understand.</p>
  
  <p>Originally I have this:  <code>rownames (mtcars[mtcars$hp &gt;150,])</code>.</p>
  
  <p>Then, rather than hard-coding "hp", I wanted to assign "hp" to a variable:  <code>foo &lt;- "hp"</code> and subset with that.  I got it working using this:  <code>rownames (mtcars[mtcars[foo] &gt;150,])</code>.  (Thanks to <a href="https://stackoverflow.com/questions/18222286/dynamically-select-data-frame-columns-using-and-a-vector-of-column-names">link</a> which stopped me from playing with the <code>$</code> operator.)</p>
  
  <p>But, as I was building up this statement, I noticed there was a difference between the two.  For <code>mtcars$hp &gt; 150</code>, I get this output:</p>
  
  <pre><code> [1] FALSE FALSE FALSE FALSE  TRUE FALSE  TRUE FALSE FALSE FALSE FALSE  TRUE
  [13]  TRUE  TRUE  TRUE  TRUE  TRUE FALSE FALSE FALSE FALSE FALSE FALSE  TRUE
  [25]  TRUE FALSE FALSE FALSE  TRUE  TRUE  TRUE FALSE
  </code></pre>
  
  <p>For <code>mtcars[foo] &gt; 150</code>, I get this:</p>
  
  <pre><code>                       hp
  Mazda RX4           FALSE
  Mazda RX4 Wag       FALSE
  Datsun 710          FALSE
  Hornet 4 Drive      FALSE
  Hornet Sportabout    TRUE
  ...
  </code></pre>
  
  <p>Are these two of the same "type"?  Is there any reason why R displays the first one without rownames and the second one with rownames?</p>
  
  <p>Perhaps I've naively thought that <code>$</code> and <code>[]</code> were more or less equivalent.  I can get the same final result, but I am curious and worried if my assumptions had been wrong.  "Fortunately", I ignored this difference and carried on and got the same final result.</p>
  
  <p>Thank you!</p>