title: R linear extrapolate missing values
body: <p>Is there a simple way to linearly extrapolate missing values in an R data frame?</p>
  
  <p>Maybe this is a trivial and often encountered problem in data preprocessing, however, after searching for quite a while, I could not find any straightforward solution.</p>
  
  <p>This question is NOT about interpolation but rather about extrapolating missing data before and after occurrences. </p>
  
  <p>Using user defined functions, this problem is solvable I know, but I am afraid that would be unnecessary in this case.</p>
  
  <p>Below is the starting input data frame, including the desired output format.</p>
  
  <p>Any help/hint is highly appreciated. Thank you very much in advance.</p>
  
  <p>Input data frame:</p>
  
  <pre class="lang-r prettyprint-override"><code>input &lt;- read.table(header=TRUE, text="
    ID1 ID2 ID3 ID4 ID5 ID6
    NA  20  NA  NA  NA  NA
    21  21  NA  NA  22  NA
    22  22  23  24  23  22
    NA  23  24  25  NA  23
    NA  24  25  26  NA  24
    NA  25  26  27  NA  25
    NA  26  27  28  NA  26
    NA  NA  28  NA  NA  27
    NA  NA  NA  NA  NA  NA
    NA  NA  NA  NA  NA  NA
  ")
  </code></pre>
  
  <p>Output data frame:</p>
  
  <pre class="lang-r prettyprint-override"><code>output &lt;- read.table(header=TRUE, text="
    ID1 ID2 ID3 ID4 ID5 ID6
    20  20  21  22  21  20
    21  21  22  23  22  21
    22  22  23  24  23  22
    23  23  24  25  24  23
    24  24  25  26  25  24
    25  25  26  27  26  25
    26  26  27  28  27  26
    27  27  28  29  28  27
    28  28  29  30  29  28
    29  29  30  31  30  29
  ")
  </code></pre>