Replace NA with average of the case before and after the NA, unless row starts or ends with NA : <p>Say I have a data.frame:</p>

<pre><code>t&lt;-c(1,1,2,4,NA,3) 
u&lt;-c(1,3,4,6,4,2)
v&lt;-c(2,3,4,NA,3,2)
w&lt;-c(2,3,4,5,2,3)
x&lt;-c(2,3,4,5,6,NA)
df&lt;-data.frame(t,u,v,w,x)
df

   t u  v w  x
1  1 1  2 2  2
2  1 3  3 3  3
3  2 4  4 4  4
4  4 6 NA 5  5
5 NA 4  3 2  6
6  3 2  2 3 NA
</code></pre>

<p>I would like to change the NAs so that the NA becomes replaced by the average of the one value before the NA and the one value after the NA. However, if a row starts with an NA I would like it to be replaced by the value that follows it. When a row ends with NA, I would like it to be replaced by the value before the NA. Thus, I would like to get the following result:</p>

<pre><code>   t u  v   w  x
1  1 1  2   2  2
2  1 3  3   3  3
3  2 4  4   4  4
4  4 6  5.5 5  5    --&gt; NA becomes average of 6 and 5
5  4 4  3   2  6    --&gt; NA becomes value of next case  
6  3 2  2   3  3    --&gt; NA becomes value of previous case
</code></pre>

<p>I have thousands of rows, so any help is very much appreciated!</p>
