mean-before-after imputation in R

前端 未结 4 820
渐次进展
渐次进展 2021-01-21 11:03

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

4条回答
  •  不思量自难忘°
    2021-01-21 11:34

    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.

提交回复
热议问题