title: How to group a vector into a list of vectors?
body: <p>I have some data which looks like this (fake data for example's sake): </p>
  
  <pre><code>dressId        color 
  6              yellow 
  9              red
  10             green 
  10             purple 
  10             yellow 
  12             purple 
  12             red 
  </code></pre>
  
  <p>where color is a factor vector. It is not guaranteed that all possible levels of the factor actually appear in the data (e.g. the color  "blue" could also be one of the levels). </p>
  
  <p>I need a list of vectors which groups the available colors of each dress:  </p>
  
  <pre><code>[[1]]
  yellow  
  
  [[2]] 
  red    
  
  [[3]] 
  green purple yellow 
  
  [[4]] 
  purple red 
  </code></pre>
  
  <p>Preserving the IDs of the dresses would be nice (e.g. a dataframe where this list is the second column and the IDs are the first), but not necessary. </p>
  
  <p>I wrote a loop which goes through the dataframe row for row, and while the next ID is the same, it adds the color to a vector. (I am sure that the data is sorted by ID). When the ID in the first column changes, it adds the vector to a list: </p>
  
  <pre><code>result &lt;- NULL 
  while(blah blah) 
  {
      some code which creates the vector called "colors" 
      result[[dressCounter]] &lt;- colors 
      dressCounter &lt;- dressCounter + 1
  }
  </code></pre>
  
  <p>After wrestling with getting all the necessary counting variables correct, I found out to my dismay that it doesn't work. The first time, <code>colors</code> is </p>
  
  <pre><code>[1] yellow
  Levels: green yellow purple red blue
  </code></pre>
  
  <p>and it gets coerced into an integer, so <code>result</code> becomes <code>2</code>. </p>
  
  <p>In the second loop repetition, <code>colors</code> only contains red, and <code>result</code> becomes a simple integer vector, <code>[1] 2 4</code>. </p>
  
  <p>In the third repetition, <code>colors</code> is a vector now, </p>
  
  <pre><code>[1] green  purple yellow
  Levels: green yellow purple red blue 
  </code></pre>
  
  <p>and I get </p>
  
  <pre><code>result[[3]] &lt;- colors
  </code></pre>
  
  <blockquote>
    <p>Error in result[[3]] &lt;- colors :<br>
          more elements supplied than there are to replace</p>
  </blockquote>
  
  <p>What am I doing wrong? Is there a way to initialize <code>result</code> so it doesn't get converted into a numeric vector, but becomes a list of vectors? </p>
  
  <p>Also, is there another way to do the whole thing than "roll my own"? </p>