I have a sequence of treatments, one per day (binary), say:
trt <- c(0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0)
I want to create a new vector, which corresponds to each element of trt
and indicates whether the NEXT three treatments are all 0. For example, the indicator vector for trt
above would be:
indicator <- c(0, 1, 1, 0, 0, 0, 1, 1, 1, NA, NA, NA)
e.g. Days 3-5 have no treatment (trt[3], trt[4], trt[5]
are all 0) so the indicator at Day 2 should equal 1 (indicator[2]
should be 1)
There are only two requirements for indicator
:
- 1 if the following three days will have no treatment
- 0 if the following three days will have at least one treatment
- Except it should be NA if there are <3 days left to observe
How would I do this in R? If this is doable without a for-loop, that would be ideal, but not absolutely necessary.