Say I have the following data.table:
library(data.table)
DT <- data.table(R=sample(0:1, 10000, rep=TRUE), Seq=0)
Which returns something like:
R Seq
1: 1 0
2: 1 0
3: 0 0
4: 0 0
5: 1 0
---
9996: 1 0
9997: 0 0
9998: 0 0
9999: 0 0
10000: 1 0
I want to generate a sequence (1, 2, 3,..., n) that resets whenever R changes from the previous row. Think of it like I'm counting a streak of random numbers.
So the above would then look like:
R Seq
1: 1 1
2: 1 2
3: 0 1
4: 0 2
5: 1 1
---
9996: 1 5
9997: 0 1
9998: 0 2
9999: 0 3
10000: 1 2
Thoughts?
set.seedhere