Data Cornering
Here is how to generate a group index in R that restarts every time there change in variables.