I\'m new in R. My question is how to impute missing value using mean of before and after of the missing data point?
example;
using the mean from the upper and lo
You are looking for Moving Average Imputation - you can use the na.ma function of imputeTS for this.
library(imputeTS)
x <- c(52, 27, NA, 23, 39, NA, NA, 33, 43)
na.ma(x, k=1, weighting = "simple")
[1] 52.00000 27.00000 25.00000 23.00000 39.00000 31.66667 38.33333 33.00000 43.00000
This produces exactly the required result. With the k parameter you specify how many neighbors on each side are taken into account for the calculation.