As mentioned, zoo
has a last-observation-carried-forward function. We can add one to it:
library(zoo)
v2 <- na.locf(v)
v2[is.na(v)] <- v2[is.na(v)] + 1L
#[1] 2 2 3 3 3 3 8 9
2
solved Fill missing value based on previous values [duplicate]
As mentioned, zoo
has a last-observation-carried-forward function. We can add one to it:
library(zoo)
v2 <- na.locf(v)
v2[is.na(v)] <- v2[is.na(v)] + 1L
#[1] 2 2 3 3 3 3 8 9
2
solved Fill missing value based on previous values [duplicate]