title: Find the last missing value by column in a data frame
body: <p>I have a data frame such  as:</p>
  
  <pre><code>&gt; set.seed(8)
  
  &gt; A &lt;- rnorm(10, 20)
  &gt; B &lt;- rnorm(10, 45)
  &gt; C &lt;- rnorm(10, 60)
  
  &gt; test1 &lt;- data.frame(A,B,C)
  &gt; test1 &lt;- round(test1, digits = 2)
  
  &gt; test1[sample(1:10, 3, F), 1] &lt;- NA
  &gt; test1[sample(1:10, 3, F), 2] &lt;- NA
  &gt; test1[sample(1:10, 3, F), 3] &lt;- NA
  
  &gt; test1[11:20, ] &lt;- NA
  &gt; test1$date &lt;- seq.Date(from = as.Date('1970-01-01', origin='01-01-1970'),
                     to = as.Date('1970-01-20', origin='01-01-1970'), by=1)
  &gt; test1
         A     B     C       date
  1  19.92    NA 61.28 1970-01-01
  2  20.84 45.29    NA 1970-01-02
  3  19.54 45.42    NA 1970-01-03
  4  19.45 43.71 60.02 1970-01-04
  5  20.74 45.07 61.74 1970-01-05
  6  19.89 44.19 58.89 1970-01-06
  7     NA    NA 58.94 1970-01-07
  8  18.91 44.73    NA 1970-01-08
  9     NA    NA 60.60 1970-01-09
  10    NA 44.76 57.98 1970-01-10
  11    NA    NA    NA 1970-01-11
  12    NA    NA    NA 1970-01-12
  13    NA    NA    NA 1970-01-13
  14    NA    NA    NA 1970-01-14
  15    NA    NA    NA 1970-01-15
  16    NA    NA    NA 1970-01-16
  17    NA    NA    NA 1970-01-17
  18    NA    NA    NA 1970-01-18
  19    NA    NA    NA 1970-01-19
  20    NA    NA    NA 1970-01-20
  </code></pre>
  
  <p>So there are many <code>NA</code> at the end (a <code>NA</code> "tail") that one may want to eliminate. I've tried finding the first <code>NA</code> but I'll bump into a <code>NA</code> in the middle of the data frame:</p>
  
  <pre><code> &gt; apply(test1, 2, function(x) which(is.na(x))[1])
     A    B    C date 
     7    1    2   NA 
   &gt; test1$A[8] #As you can see this doesn't give me what I need.
   [1] 18.91
  </code></pre>
  
  <p>The <code>complete.cases()</code> function won't help since I don't want to eliminate all rows with missing values. Inverting the data frame and retrieving the first non <code>NA</code> should be the way to go here:</p>
  
  <pre><code>&gt; apply(test1[nrow(test1):1, ], 2, function(x) which(!is.na(x))[1])
  
     A    B    C date 
    13   11   11    1 
  </code></pre>
  
  <p>After this the column with the smallest number should be used to find the row number that contains the last non completely <code>NA</code> row. This leads to:</p>
  
  <pre><code>&gt; test1[1:(nrow(test1)-10), ]
  </code></pre>
  
  <p>Which will produce a data frame with no long missing value tail. Although this works I reckon that there is a better/faster way to accomplish this. I'll write a function and wait for an answer.  </p>