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
- 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(!is.na(input[c(rbind(which(is.na(c(input)))-1, which(is.na(c(input)))+1))]), rep(1:(length(!is.na(input[c(which(is.na(c(input)))-1, which(is.na(c(input)))+1)]))/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.