My dataset looks like the following (let's call it "a"):
date value
2013-01-01 12.2
2013-01-02 NA
2013-01-03 NA
2013-01-04 16.8
2013-01-05 10.1
2013-01-06 NA
2013-01-07 12.0
I would like to replace the NA
by the mean of the closest surroundings values (the previous and the next values in the series).
I tried the following but I am not convinced by the output...
miss.val = which(is.na(a$value))
library(zoo)
z = zoo(a$value, a$date)
z.corr = na.approx(z)
z.corr[(miss.val - 1):(miss.val + 1), ]