 <p>You can evaluate <code>data$err[i]</code> in the outer loop.</p> <pre><code>for (i in 1:nrow(data)){ n.tmp &lt;- d.tmp &lt;- 0 datai &lt;- data$err[i] for (j in i:nrow(data)){ wt &lt;- some.step.function(data$X[j] + datai) n.tmp &lt;- n.tmp + data$Y[j] / wt d.tmp &lt;- d.tmp + 1 / wt } n[i] &lt;- n.tmp d[i] &lt;- d.tmp } </code></pre>