I have a vector that looks something like this:
c(0.5,0,0,0,0,0.7,0,0,0,0,0.4,0,0,0,0)
Suppose I want to copy the values on positions 1, 6
Another possibility:
vec <- c(0.5,0,0,0,0,0.7,0,0,0,0,0.4,0,0,0,0) library(zoo) vec[vec==0] <- NA na.locf(vec) #[1] 0.5 0.5 0.5 0.5 0.5 0.7 0.7 0.7 0.7 0.7 0.4 0.4 0.4 0.4 0.4