The goal is to check if the value in the index i is 1, and then make the previous six entries equal to 1.
x <- c(0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1) ## Required output y <- c(1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1) ## Attempt for(j in seq_along(x)){ if(x[j] == 1){ for(i in (j-6):j) x[i] = 1 }}
Could you help solve this or the best approach?
Thanks.