title: Fill NA with previous value based on multiple column
body: <p>Im new with R.</p>
  
  <p>Currently i'm working with dplyr package for manipulation data.
  But i'm stuck when want to do some calculation as below:</p>
  
  <pre><code>library(dplyr)
  w18 &lt;- c(1,2,3,4,5)
  w19 &lt;- c(10,10,NA,NA,NA)
  temp_df &lt;- data.frame(w18,w19)
  </code></pre>
  
  <p>I expect NA will replaced with value from <code>w19 &lt;- ifelse(is.na(w19), lag(w19)+ lag(w18) else w19</code></p>
  
  <p>how ever when im tried with my code as below:</p>
  
  <pre><code>w19_function &lt;- function(temp_df) {
  isna &lt;- is.na(temp_df)
  lag_w19 &lt;- tail(temp_df[!isna],1)
  loc &lt;- length(w18[!is.na(w18),])
  temp_df[isna] &lt;- lag_w19+ temp_df[loc,'w18']
  return(temp_df)
  }
  w19_function(temp_df)
  </code></pre>
  
  <p>i expect result like this one :</p>
  
  <pre><code>w18,w19
  1,10
  2,10
  3,12
  4,15
  5,19
  </code></pre>
  
  <p>but the code giving result :</p>
  
  <pre><code>w18,w19
  1,10
  2,10
  3,12
  4,12
  5,12
  </code></pre>
  
  <p>what thing I should to add? please help me for solving this case.</p>