title: replacing a missing value in R with average value
body: <p>I have a dataframe with columns of data with missing value and I would like to replace the missing value by taking the mean using the value of the cells above and below.</p>
  
  <pre><code> df1&lt;-c(2,2,NA,10, 20, NA,3)
   if(df1[i]== NA){
   df1[i]= mean(df1[i+1],df1[i-1])
  }
  </code></pre>
  
  <p>However, I am getting this error </p>
  
  <pre><code>  Error in if (df1[i] == NA) { : missing value where TRUE/FALSE needed
    In addition: Warning message:
    In if (df1[i] == NA) { :
    the condition has length &gt; 1 and only the first element will be used
  </code></pre>
  
  <p>Any guidance would be appreciated to solve this issue. </p>