Assume the data look like:
df <- data.frame(ID=1:6, Value=c(NA, 1, NA, NA, 2, NA))
df
ID Value
1 1 NA
2 2 1
3 3 NA
4 4 NA
5 5 2
6 6 NA
And I want the imputed result be like:
ID Value
1 1 1.0
2 2 1.0
3 3 1.5
4 4 1.5
5 5 2.0
6 6 2.0
More specific, I want to impute missing data with mean of first previous and latter non missing data, if only one of previous or latter non missing data exist, impute with this non missing data. Behavior for all data are missing is not defined.
How can I do that in R?