I have a variable x. I would like to obtain a variable z such that it takes a unique identifier (here a letter, but it could be a number) according to these conditions:
- it takes a unique identifier if the observation of before is zero and the current is one,
- the same identifier of previous observation if the current and past values are one,
- the value NA if the current observation is zero,
(=1.) a new unique identifier if the observation of before is zero and the current is one, etc.:
x z 0 NA 1 A 1 A 1 A 0 NA 1 B 1 B 0 NA
Anyone might have an idea as how to do it?