I have data similar to the following:
A= c(0,0,0,-1,0,0,0,1,1,1,0,0,-1,0,0,-1,-1,1,1,1,-1,0,0,0,-1,0,0,-1,-1,1,1,0,0,0,0,1,-1)
The goal is to extract alternating -1s and 1s. I want to create a function in which the input vector contains 0.1 and -1. The output ideally selects all 0s and alternates -1s and 1s.
For example, the desired result for the above example:
B= c(0,0,0,-1,0,0,0,1,0,0,0,0,-1,0,0,0,0,1,0,0,-1,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,-1)
Two 1s in 9th and 10th places are rotated by 0, because we only save the first 1 or -1. For this reason, -1s in the 16th and 17th positions of A for this reason also rotate to 0.
Does anyone have a good idea to create such a function?