title: how to fill missing values in a vector with the mean of value before and after the missing one
body: <p>Currently I am trying to impute values in a vector in R. The conditions
  of the imputation are. </p>
  
  <ul>
  <li>Find all NA values</li>
  <li>Then check if they have an existing value before and after them</li>
  <li>Also check if the value which follows the NA is larger than 
  the value before the NA</li>
  <li>If the conditions are met, calculate a mean taking the values before
  and after. </li>
  <li>Replace the NA value with the imputed one</li>
  </ul>
  
  <pre><code># 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)
  
  </code></pre>
  
  <p>I started out to write code to detect the values which can 
  be imputed. But I got stuck with the following. </p>
  
  <pre><code># 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)
  </code></pre>
  
  <p>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.</p>
  
  <p>Any help with this would be highly appreciated.</p>