title: Subset a dataframe using a logical vector with $
body: <p>I'm having trouble understanding both the <strong>reason for use</strong> and <strong>behavior</strong> of the <code>$</code> symbol in subsetting a <code>data.frame</code> in R. The following example was presented in a beginner's class I'm taking (not with a live professor so can't ask there):</p>
  
  <pre><code>temp_mat &lt;- matrix(1:9, nrow=3)
  colnames(temp_mat) &lt;- c('a', 'b', 'c')
  temp_df &lt;- data.frame(temp_mat)
  </code></pre>
  
  <p>Calling <code>temp_df</code> obviously outputs:</p>
  
  <pre><code>  a b c
  1 1 4 7
  2 2 5 8
  3 3 6 9
  </code></pre>
  
  <p>The example given in the course is then:</p>
  
  <pre><code>temp_df[temp_df$c &lt; 10]
  </code></pre>
  
  <p>Which outputs:</p>
  
  <pre><code>  a b c
  1 1 4 7
  2 2 5 8
  3 3 6 9
  </code></pre>
  
  <p><strong>Reason for use question:</strong> The course indicates that <code>$</code> is used for partial matching, and that <code>x$y</code> is an exact substitute for <code>x[["y", exact=FALSE]]</code>. Why would we want to use a partial matching operator here? Do we use it because we know for sure that in our <code>temp_df</code> there is no other column similar to "c" that could be mistakenly picked up? Additionally, how is partial match measured? A minimum % of characters matching or something? It appears there is a <code>getElement</code> function that would be much more appropriate if working with datasets with unknown or similar column names (e.g. Home Phone versus Cell Phone, would these be seen as a valid partial match?)</p>
  
  <p><strong>Behavior question:</strong> it appears the above example <code>temp_df[temp_df$c &lt; 10]</code> is saying "return the subset of elements from temp_df where column c is less than 10" and because all column c elements meet the criteria, the entire dataframe is returned. My interpretation is obviously wrong because <code>temp_df[temp_df$c &lt; 9]</code> returns:</p>
  
  <pre><code>  a b
  1 1 4
  2 2 5
  3 3 6
  </code></pre>
  
  <p>Although the row 1 and 2 elements in column c do meet the criteria of being less than 9, the entire column is omitted. My question then becomes twofold: what is that logical vector actually saying/doing? And how would I write my interpretation of "return the subset of elements from temp_df where column c is less than 9" and have it return:</p>
  
  <pre><code>  a b c
  1 1 4 7
  2 2 5 8
  </code></pre>
  
  <p>Because in my mind, elements 1 and 2 (rows 1 and 2) met that criteria as their column c values are less than 9 and thus should be returned.    </p>