title: Using nested apply functions instead of nested for loops
body: <p>My objective here was to iterate across each column in a <code>df</code> and then for each column iterate down each row and perform a function. The specific function in this case replaces the <code>NA</code> values with the corresponding value in the final column, but the details of the function required are not relevant to the question here. I got the results I needed using two nested for loops like this:</p>
  
  <pre><code>for (j in 1:ncol(df.i)) {
    for (i in 1:nrow(df.i)) {
      df.i[i,j] &lt;- ifelse(is.na(df.i[i,j]), df.i[i,39], df.i[i,j])
    }
  }
  </code></pre>
  
  <p>However, I believe this should be possible using an <code>apply(df.i, 1, function)</code> nested within an <code>apply(df.i, 2, function)</code> But I'm not totally sure that is possible or how to do it. Does anyone know how to achieve the same thing with a nested use of the <code>apply</code> function?</p>