mean-before-after imputation in R

前端 未结 4 857
渐次进展
渐次进展 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:28

    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

提交回复
热议问题