title: Split data frame by two factors
body: <p>I have a data frame (<code>sampdata</code>) that looks something like this:</p>
  
  <pre><code>A B  C   D
  1 X  5 0.3
  2 Y 10 0.9
  3 Y  7 0.2
  4 Y  5 0.4
  5 X 10 0.7
  </code></pre>
  
  <p>Basically, I want to create two new data frames based on both column B and C.  On earlier posts I have seen how to subset the data using 'split' based on one factor which I did do</p>
  
  <pre><code>test &lt;- split(sampdata, sampdata$B)
  str(test)
  </code></pre>
  
  <p>So far so good. But, when I tried to add in a second split:</p>
  
  <pre><code>testBC &lt;- split(test, test$C)
  </code></pre>
  
  <p>I received an error message:</p>
  
  <blockquote>
    <p>Error in split.default(test, test$Product) : 
      group length is 0 but data length > 0</p>
  </blockquote>
  
  <p>I also tried:</p>
  
  <pre><code>testBC &lt;- split(test$B, test$C)
  </code></pre>
  
  <p>but got another error message. So, then I tried a second method, based on <code>ddply</code> and <code>plyr</code> package:</p>
  
  <pre><code>test2 &lt;- ddply(sampdata, c("B", "C"))
  </code></pre>
  
  <p>This did organize the data by row such that: </p>
  
  <pre><code>A B  C   D
  1 X  5 0.3
  5 X 10 0.7 
  2 Y 10 0.9
  3 Y  7 0.2
  4 Y  5 0.4
  </code></pre>
  
  <p>However, other threads only show how to access a specific data frame based on one col (<code>test2$B</code>) but not both. I would prefer to simply generate a new data frame based on a subset of B and C such that:</p>
  
  <pre><code>newdf1
  A B C   D
  1 X 5  .3
  5 X 10 .9
  
  newdf2
  A B C   D
  2 Y 7  .2
  3 Y 5  .4
  4 Y 10  .7
  </code></pre>
  
  <p>After trying a couple methods what is likely a straightforward/simple task is surprisingly difficult (for me at least).</p>
  
  <p>Any help most appreciated.</p>