Q:

mean-before-after imputation in R

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 lower of each NA as the impute value.
-mean for row number 3 is 38.5
-mean for row number 7 is 32.5
age
52.0
27.0
NA
23.0
39.0
32.0
NA
33.0
43.0

Thank you.

A:

Here a solution using from na.locf from zoo package which replaces each NA with the most recent non-NA prior or posterior  to it.
0.5*(na.locf(x,fromlast=TRUE) + na.locf(x))
[1] 52.0 27.0 25.0 23.0 39.0 32.0 32.5 33.0 43.0

the advantage here if you have more than one consecutive NA.
x <- c(52, 27, NA, 23, 39, NA, NA, 33, 43)
0.5*(na.locf(x,fromlast=TRUE) + na.locf(x))
[1] 52 27 25 23 39 36 36 33 43

EDIT
rev argument is deprecated so I replace it by fromlast

