Search code examples
rvectorlaglead

Leading vector taking values based on future values of another vector


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.


Solution

  • You can use zoo::rollsum to check whether 3 consecutive days add to zero. You'll also need to use dplyr::lead to account for the fact that you are looking at the next three days. Finally, you need to convert the resulting logical vector to numeric.

    Anyway, that all boils down to this one-liner (without loops)

    +(dplyr::lead(zoo::rollsum(trt, 3, fill = NA, align = "left")) == 0)
    #> [1]  0  1  1  0  0  0  1  1  1 NA NA NA