Reputation: 306
In my setup, every month a city might observe the occurrence of an event or not. I want to count how many of these events have occurred in a row (in sequence) within each city.
I managed to implement a script with 2 for loops one inside each other, but I have over 1500 cities and around 250 months, so it took forever for the loops to conclude. Therefore, I'm looking for some faster and more elegant solution.
df <- data.frame(city = c(rep("a",10),rep("b",10)), month = c(1:10,1:10), event = c(0,0,0,1,0,1,1,1,0,1,1,1,0,0,0,1,1,1,1,0), desirable_output= c(0,0,0,1,0,1,2,3,0,1,1,2,0,0,0,1,2,3,4,0))
In this toy example, the column "desirable_output" shows my expected output.
Thanks in advance for any ideas.
Upvotes: 0
Views: 65
Reputation: 14764
Try:
library(data.table)
setDT(df)[, desirable_output := cumsum(event), by = .(city, rleid(city, event))]
Upvotes: 1