I am interested in generating two variable counting how many times a given value is repeated and how many times it is repeated consecutively.
For example, in the above dataset, I need to count how many times 1 is repeated and repeated consecutively in the variable X, and these countings are placed in the new variables n and rep, respectively.
id X n rep
1 0 0 0
2 1 1 1
3 0 0 0
4 1 2 1
5 0 0 0
6 1 3 2
7 1 3 2
8 0 0 0
9 1 4 2
10 1 4 2
Any ideas or suggestions?
Huge thanks!
For example, in the above dataset, I need to count how many times 1 is repeated and repeated consecutively in the variable X, and these countings are placed in the new variables n and rep, respectively.
id X n rep
1 0 0 0
2 1 1 1
3 0 0 0
4 1 2 1
5 0 0 0
6 1 3 2
7 1 3 2
8 0 0 0
9 1 4 2
10 1 4 2
Any ideas or suggestions?
Huge thanks!
Comment