title: Function to group elements of a vector/list
body: <p>Suppose I have a vector such as </p>
  
  <pre><code>vec &lt;- 0:10
  </code></pre>
  
  <p>and a function such as </p>
  
  <pre><code>f &lt;- function (x) x %% 3
  </code></pre>
  
  <p>Is there a builtin function in R that will break this vector into groups based on the return value of <code>f</code> when applied to each element?  If not, what is the most straightforward way to implement this using built-ins?</p>
  
  <p>The expected return value is</p>
  
  <pre><code>list(c(0,3,6,9), c(1,4,7,10), c(2,5,8))
  </code></pre>
  
  <p>For a more complicated example, take a vector of strings, and group together those which are anagrams (i.e. they consist of the same letters). In this case, <code>f</code> would take a string, lowercase it, and sort it.   On Unix systems there's a word list available:</p>
  
  <pre><code>words &lt;- scan('/usr/share/dict/words', what=character, sep='\n')
  </code></pre>
  
  <p>I do not insist on using these particular data structures (vector, list), if they are not the most appropriate for this task in R.</p>