I would to fill in up to let's say 3 consecutive NAs in r. I want to fill in even those that reach the limit point, but just to fill in up to 3 consecutive NAs. For instance. Let's say that I have the following object:
x<-c(1.6533926 , 1.9263536 , NA , NA, 0.6378220, -0.1162299, -0.5909386, -0.4997750, 0.2603278, 0.8183298, NA, NA, NA,NA,NA,NA)
I know that there is a function called na.locf, with the option maxgap. Though, maxgap discards those cases where the number of consecutive nas reached a limit.
na.locf(y, maxgap=3)
I would like instead to fill in up to 3 consecutive NAs. Then I should have the following. Aditionally. I would like to keep the NAs that are not fill in.
y<-c(1.6533926 , 1.9263536 , 1.9263536 ,1.9263536, 0.6378220, -0.1162299, -0.5909386, -0.4997750, 0.2603278, 0.8183298, 0.8183298, 0.8183298, 0.8183298,NA,NA,NA)
In addition I would like to specify that I am using this in the context of a data table. Where my original code looks in the following way:
df[, lapply(.SD, function(x){na.locf(x,na.rm=FALSE)})]
Thank you!