I have a dataframe of classes, sorted by the number of periods or blocks in a day. I'd like to have another variable that shows that every group of classes as a series, but only if they are one after another. So if there are two math classes in period 4 and 5, that would be one group, while the math in period 7 and 8 would be a different group. I'm interested in a dplyr method, but other methods will work as well.
I've tried to do group_by with mutate, but I'm missing a step.
df <- data.frame(
period = c(1:8),
classes = c("hist", "hist", "hist",
"math", "math",
"physics",
"math", "math")
)
I want the following output:
df <- data.frame(
period = c(1:8),
classes = c("hist", "hist", "hist",
"math", "math",
"physics",
"math", "math")
series = c(1, 1, 1, 2, 2, 3, 4, 4)
)