title: Optimize this for-loop procedure to avoid a program crash
body: <p>I'm struggling trying to perform an operation in R without it crashing. I give you a reproducible example. I have X:</p>
  
  <pre><code>X &lt;- data.frame(V1 = c("chr1", "chr1", "chr1", "chr1", "chr1"),
                  Start = c(0, 1001, 3002, 4059, 6581),
                  Stop = c(1000, 3001, 4058, 6580, 7002),
                  A = c(10, 4, 5, 6, 9),
                  B = c(923, 39, 5, 9, 93),
                  C = c(239, 2, 13, 4, 5))
  </code></pre>
  
  <p>And I want to perform this operation:</p>
  
  <pre><code>for (row in 1:nrow(X)){
    X$A &lt;- (X$A / (X[row, "Stop"] - X[row, "Start"])) * mean(X$Stop - X$Start)
    X$B &lt;- (X$B / (X[row, "Stop"] - X[row, "Start"])) * mean(X$Stop - X$Start)
    X$C &lt;- (X$C / (X[row, "Stop"] - X[row, "Start"])) * mean(X$Stop - X$Start)
  }
  </code></pre>
  
  <p>My problem appears when my document is a lot larger (like 2.000.000 rows). Is there any way to do this operation faster in such a large data.frame?</p>