# dataframe
df <- tibble(
# groups
group = c("A", "A", "A", "B", "A", "A", "A", "C", "B", "B", "C", "A"),
# running count of all groups
count = c(1,1,1,2,3,3,3,4,5,5,6,7)
In my data there are a series of repeating groups. In this example, rows 1-3 are the first occurrence of group “A”, row 4 is the first occurrence of group “B”, rows 5-7 are the second occurrence of group “A”, row 8 is the first occurrence of group “C” etc. count
is the running count of all groups.
I want a running count of each individual group occurrence that provides the output seen in individual_runing_count
. The order of groups
and number of occurrences of each group will not always be the same, so I can’t use a fixed criteria such as number of rows or order of the groups
column.
# df with required output
df <- tibble(
# groups
group = c("A", "A", "A", "B", "A", "A", "A", "C", "B", "B", "C", "A"),
# order in which groups occurred
order = c(1,1,1,2,3,3,3,4,5,5,6,7),
# required column output
individual_running_count = c(1,1,1,1,2,2,2,1,2,2,2,3)
)
user25035369 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.