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
This would be a basic manual approach you can take:
age <- c(52, 27, NA, 23, 39, 32, NA, 33, 43)
age[is.na(age)] <- rowMeans(cbind(age[which(is.na(age))-1],
age[which(is.na(age))+1]))
age
# [1] 52.0 27.0 25.0 23.0 39.0 32.0 32.5 33.0 43.0
Or, since you seem to have a single column data.frame
:
mydf <- data.frame(age = c(52, 27, NA, 23, 39, 32, NA, 33, 43))
mydf[is.na(mydf$age), ] <- rowMeans(
cbind(mydf$age[which(is.na(mydf$age))-1],
mydf$age[which(is.na(mydf$age))+1]))