title: Creating Conditional Indices on Transactional Data in R
body: <p>I have a data set with transaction data in order.  I have Field1, Field2, Field3, and Numeric1 already.  How do I create Index1 &amp; Index2 in the table below?  First A/BB/CCC shows up (Index1 = 1) five times (Index2 = 1 through 5).  Next, A/BB/ddd shows up for the first time (Index1 = 1) with three records (Index2 = 1 through 3).  After that, A/BB/CCC comes back again for four more records (Index1 = 2 and Index2 = 1 through 4).  This interchanging occurs until Field1 = A no longer exists.  Now we move on to Field1 = T and the whole process starts over.  </p>
  
  <p>For an example, imagine two people are playing mini golf.  Field1 represents the date of each game, Field2 &amp; Field3 represent categorical variables, Numeric1 represents distance.  Index1 would represent which hole they are on.  Index2 would represent the number of strokes they took.</p>
  
  <p>Thanks for your help.</p>
  
  <pre><code>Field1  Field2  Field3  Numeric1   Index1  Index2
    A       BB      CCC       4         1       1
    A       BB      CCC       2         1       2
    A       BB      CCC      12         1       3
    A       BB      CCC      25         1       4
    A       BB      CCC      -4         1       5
  
    A       BB      ddd       30        1       1
    A       BB      ddd       8         1       2  
    A       BB      ddd       1         1       3 
  
    A       BB      CCC       4         2       1
    A       BB      CCC      21         2       2
    A       BB      CCC      12         2       3
    A       BB      CCC      -4         2       4
  
    A       BB      ddd      25         2       1
    A       BB      ddd       9         2       2  
    A       BB      ddd      -1         2       3 
  
    A       BB      CCC       9         3       1
    A       BB      CCC      -3         3       2
    A       BB      CCC       1         3       3
  
    A       BB      ddd       5         3       1
  
    A       BB      CCC       3         4       1
    A       BB      CCC       3         4       2
    A       BB      CCC       4         4       3
    A       BB      CCC      20         4       4
    A       BB      CCC      51         4       5
  
     ...
  
    T       BB      CCC       6         1       1
    T       BB      CCC       5         1       2
    T       BB      CCC      13         1       3
  </code></pre>
  
  <p>Code to reproduce (from @michaelvine):</p>
  
  <pre><code>  Field1&lt;-rep("A", 24)
    Field2&lt;-rep("BB", 24)
    Field3&lt;-c(rep("CCC", 5), rep("ddd",3),rep("CCC", 4), rep("ddd",3),rep("CCC", 3), rep("ddd",1),rep("CCC", 5))
    Numeric1&lt;-c(4,2,12,25,-4,6,2,1,4,21,12,-4,6,2,1,9,-3,1,-4,3,3,4,20,51)
    Test_df&lt;-data.frame(Field1,Field2,Field3,Numeric1)
  </code></pre>