title: Using na.locf to carry last value forward ignoring first rows when first observation is na
body: <p>I would like to make use of the <a href="https://www.rdocumentation.org/packages/zoo/versions/1.8-0/topics/na.locf" rel="nofollow noreferrer"><code>na.locf</code></a> to carry forward non-missing values for data frames where <em>first observation may be zero</em>.</p>
  
  <h1>Problem</h1>
  
  <pre><code>dta &lt;- data.frame(A = c(NA, NA, 1, 2, 4, 5, NA, NA, NA),
                    B = c(NA, 5, 4, 5, 8, 9, NA, NA, 100))
  dta %&gt;% mutate_all(.funs = funs(na.locf(.)))
  </code></pre>
  
  <blockquote>
    <p>Error in <code>mutate_impl(.data, dots)</code> :    Column <code>A</code> must be length 9
    (the number of rows) or one, not 7</p>
  </blockquote>
  
  <h1>Desired results</h1>
  
  <pre><code>Vectorize(require)(package = c("dplyr", "zoo"),
                     character.only = TRUE)
  
  dta &lt;- data.frame(A = c(0, NA, 1, 2, 4, 5, NA, NA, NA),
                    B = c(0, 5, 4, 5, 8, 9, NA, NA, 100))
  dta %&gt;% mutate_all(.funs = funs(na.locf(.)))
  </code></pre>
  
  <h1>Workaround</h1>
  
  <p>The potential workaround would could involve replacing first set of <code>NAs</code> with zeros and carrying zero forward that could be later replaced but I'm interested in leaving NAs where they are and exploring if there is a convenient way to make <code>na.locf</code> ignore situations where the function did not receive non-NA value to start replacing. </p>