how to fill missing values in a vector with the mean of value before and after the missing one

Currently I am trying to impute values in a vector in R. The conditions
of the imputation are.

  • Find all NA values
  • Then check if they have an existing value before and after them
  • Also check if the value which follows the NA is larger than
    the value before the NA
  • If the conditions are met, calculate a mean taking the values before
    and after.
  • Replace the NA value with the imputed one
# example one
input_one = c(1,NA,3,4,NA,6,NA,NA)

# example two
input_two = c(NA,NA,3,4,5,6,NA,NA)

# example three
input_three = c(NA,NA,3,4,NA,6,NA,NA)

I started out to write code to detect the values which can
be imputed. But I got stuck with the following.

# incomplete function to detect the values
sapply(split(![c(rbind(which(, which(]), 
             rep(1:(length(![c(which(, which(]))/2), each = 2)), all)

This however only detects the NAs which might be
imputable and it only works with example one. It is incomplete and
unfortunately super hard to read and understand.

Any help with this would be highly appreciated.

Leave a Reply

Your email address will not be published. Required fields are marked *