I want to perform some summary statistics over groups in time series. Here is what I have
test = data.table(ind=c(1:8), val = c(0,0,1,1,1,0,0,1))
ind val
1: 1 0
2: 2 0
3: 3 1
4: 4 1
5: 5 1
6: 6 0
7: 7 0
8: 8 1
How do I create third column which will be my group index? like this -
ind val group_id
1: 1 0 1
2: 2 0 1
3: 3 1 2
4: 4 1 2
5: 5 1 2
6: 6 0 3
7: 7 0 3
8: 8 1 4
I'm interested in efficient solution (as real life table is rather big) and would love to avoid straightforward for loop