title: How to reshape using two variables
body: <p>Suppose I have this data:</p>
  
  <pre><code>group    obs    data    data_A    data_B
  1        1      7_a     7_a       
  1        2      4_b               4_b  
  1        3      1_a     1_a     
  2        1      5_b               5_b
  3        1                  
  4        1      3_b               3_b
  4        2      4_b               4_b
  4        3      9_a     9_a     
  4        4      8_b               8_b   
  </code></pre>
  
  <p><code>data_A</code> and <code>data_B</code> are constructed based on <code>data</code>. They follow the rule that they take on the value of <code>data</code> if <code>data</code> ends in <code>a</code> for <code>data_A</code> and <code>b</code> for <code>data_B</code>; if data is blank, <code>data_A</code> and <code>data_B</code> both stay blank. </p>
  
  <p>I would like to reshape the data to be as follows:</p>
  
  <pre><code>group    data_A1    data_A2    data_B1    data_B2    data_B3
  1        7_a        1_a        4_b                     
  2                              5_b              
  3                                            
  4        9_a                   3_b        4_b         8_b    
  </code></pre>
  
  <p>where the number of columns is automatically determined by the number of values.</p>
  
  <p><code>7_a</code> and <code>9_a</code> are in <code>data_A1</code> because they are the first instance of an <code>a</code> variable in their respective groups. <code>1_a</code> is in <code>data_A2</code> because it is the second instance of an <code>a</code> variable in its group, and so on.</p>
  
  <p>How can this be done?</p>
  
  <p>(I know about <code>reshape</code> and that this can be used in similar situations.)</p>