title: Looping through consecutive columns of a dataframe
body: <p>I am trying to loop through a dataframe of columns, and the calculated result is deposited into a matrix.</p>
  
  <p>The scenario can be replicated with the following sample data:</p>
  
  <pre><code>df = data.frame(replicate(10,sample(0:20,10,rep=TRUE))) # the columns to be calculated on
  
  M1 = as.data.frame(matrix(0, nrow = 10, ncol = 10)) # a matrix to hold the results.
  rownames(M1) = colnames(df)
  colnames(M1) = colnames(df)
  </code></pre>
  
  <p>And appear as follows:</p>
  
  <pre><code>&gt; df # Frame with columns of data, X1 to X10
  
     X1 X2 X3 X4 X5 X6 X7 X8 X9 X10
  1   1 19  2  6  6  5  0  2  5  10
  2  16  7 14 16 16 18 11  2 18  11
  3   7  6 11  4  4  1 18 11 10  16
  4  20  2  4 20  4  6 10  5 16   7
  5   9  8 16 19 11  2 14  7 13   7
  6   5 16  6  8 20 15  5 11  4   0
  7  11 16 12  8 18 20 20 20 10  14
  8  17 14 10  4  3 10 13 11  5   1
  9   9 20 10  5  1  7 12 10  5   6
  10  8 14  3 14 20 10 17 20  9  14
  
  &gt; M1 # Output frame to hold results
  
      X1 X2 X3 X4 X5 X6 X7 X8 X9 X10
  X1   0  0  0  0  0  0  0  0  0   0
  X2   0  0  0  0  0  0  0  0  0   0
  X3   0  0  0  0  0  0  0  0  0   0
  X4   0  0  0  0  0  0  0  0  0   0
  X5   0  0  0  0  0  0  0  0  0   0
  X6   0  0  0  0  0  0  0  0  0   0
  X7   0  0  0  0  0  0  0  0  0   0
  X8   0  0  0  0  0  0  0  0  0   0
  X9   0  0  0  0  0  0  0  0  0   0
  X10  0  0  0  0  0  0  0  0  0   0
  </code></pre>
  
  <p>In <code>df</code> columns X1 and X2 are in put to the calculation, then X1 and X3, then X1 and X4, etc. Then the loop will cycle X2 and X3, then X2 and X4, etc.</p>
  
  <p>Columns <em>n</em> and <em>m</em> are entered into the calculation/loop and the result should be placed into the appropriate place in the matrix that corresponds to column <em>n</em> x <em>m</em>.  The calculation itself simplLY determines the area between Xn and Xm as plotted lines. I'm not sure how to correctly construct the loop to do this:</p>
  
  <pre><code>  # The first iteration of the calculation, column X1 and X2 (X1 and X1 would = 0)
  
    y = seq(1,10,1)
    f1 = approxfun(y, df[,1] - df[,2]) # takes two columns as inputs
    f2 = function(x) abs(f1(x))
  
    area1 = integrate(f2, 1, 10, subdivisions = 500)
    M1[2,1] = area1$value
  </code></pre>
  
  <p>The resultant frame would produce a "half-matrix" (that is all that is needed as the mirrored half would be identical):</p>
  
  <pre><code>&gt; M1
      X1 X2 X3 X4 X5 X6 X7 X8 X9 X10
  X1   0  0  0  0  0  0  0  0  0   0
  X2   A  0  0  0  0  0  0  0  0   0
  X3   A  A  0  0  0  0  0  0  0   0
  X4   A  A  A  0  0  0  0  0  0   0
  X5   A  A  A  A  0  0  0  0  0   0
  X6   A  A  A  A  A  0  0  0  0   0
  X7   A  A  A  A  A  A  0  0  0   0
  X8   A  A  A  A  A  A  A  0  0   0
  X9   A  A  A  A  A  A  A  A  0   0
  X10  A  A  A  A  A  A  A  A  A   0
  </code></pre>
  
  <p>I began constructing a for loop but I'm tripping up on using i and j to keep X1 in until it has cycled through X2-X10, then moving on to X2, etc.</p>
  
  <p>Thanks!</p>