title: What's the use of which?
body: <p>I'm trying to get a handle on the ubiquitous <code>which</code> function.  Until I started reading questions/answers on SO I never found the need for it.  And I still don't.</p>
  
  <p>As I understand it, <code>which</code> takes a Boolean vector and returns a weakly shorter vector containing the indices of the elements which were true:</p>
  
  <pre><code>&gt; seq(10)
   [1]  1  2  3  4  5  6  7  8  9 10
  &gt; x &lt;- seq(10)
  &gt; tf &lt;- (x == 6 | x == 8)
  &gt; tf
   [1] FALSE FALSE FALSE FALSE FALSE  TRUE FALSE  TRUE FALSE FALSE
  &gt; w &lt;- which(tf)
  &gt; w
  [1] 6 8
  </code></pre>
  
  <p>So why would I ever use <code>which</code> instead of just using the Boolean vector directly?  I could maybe see some memory issues with huge vectors, since <code>length(w)</code> &lt;&lt; <code>length(tf)</code>, but that's hardly compelling.  And there are some options in the help file which don't add much to my understanding of possible uses of this function.  The examples in the help file aren't of much help either.</p>
  
  <p><strong>Edit for clarity</strong>-- I understand that the <code>which</code> returns the indices.  My question is about two things: <strong>1)</strong> why you would ever need to use the indices instead of just using the boolean selector vector? and <strong>2)</strong> what interesting behaviors of <code>which</code> might make it preferred to just using a vectorized Boolean comparison?</p>