title: Need to vectorize function that using loop (replace NA rows with values from vector)
body: <p>How I can rewrite this function to vectorized variant. As I know, using loops are not good practice in R:</p>
  
  <pre><code># replaces rows that contains all NAs with non-NA values from previous row and K-th column
  na.replace &lt;- function(x, k) {
      for (i in 2:nrow(x)) {
          if (!all(is.na(x[i - 1, ])) &amp;&amp; all(is.na(x[i, ]))) {
                  x[i, ] &lt;- x[i - 1, k]
          }
      }
      x
  }
  </code></pre>
  
  <p>This is input data and returned data for function:</p>
  
  <pre><code>m &lt;- cbind(c(NA,NA,1,2,NA,NA,NA,6,7,8), c(NA,NA,2,3,NA,NA,NA,7,8,9))
  m
        [,1] [,2]
   [1,]   NA   NA
   [2,]   NA   NA
   [3,]    1    2
   [4,]    2    3
   [5,]   NA   NA
   [6,]   NA   NA
   [7,]   NA   NA
   [8,]    6    7
   [9,]    7    8
  [10,]    8    9
  
  na.replace(m, 2)
        [,1] [,2]
   [1,]   NA   NA
   [2,]   NA   NA
   [3,]    1    2
   [4,]    2    3
   [5,]    3    3
   [6,]    3    3
   [7,]    3    3
   [8,]    6    7
   [9,]    7    8
  [10,]    8    9
  </code></pre>