Consider the following table for example:
Group<-c("AGroup", "AGroup", "AGroup", "AGroup", "BGroup", "BGroup", "BGroup", "BGroup", "CGroup", "CGroup", "CGroup", "CGroup")
Status<-c("Low", "Low", "High", "High", "High", "Low", "High", "Low", "Low", "Low", "High", "High")
df<-data.frame(Group, Status)
df$CountByGroup<-c(1, 2, 1, 2, 1, 1, 1, 1, 1, 2, 1, 2)
This creates the following table:
Group Status CountByGroup
AGroup Low 1
AGroup Low 2
AGroup High 1
AGroup High 2
BGroup High 1
BGroup Low 1
BGroup High 1
BGroup Low 1
CGroup Low 1
CGroup Low 2
CGroup High 1
CGroup High 2
The CountByGroup column is what I am trying to create. Here you can see that "Low" appeared once so far for the "AGroup" in the first row, so it has an entry of 1. "Low" directly follows the same entry "Low" in the second row, so it has an entry of 2. If it were to appear a third time in a row in the third row, CountByGroup would display an entry of 3.
We're also grouping these "Group", so the first entry for a new group is always 1 since it is the first time any entry has appeared for the group.