title: Repeatedly do a calculation on columns or rows + store results in data frame
body: <p>I wish to do calculations on elements within columns. Starting in the first row until the last row. Doing this on several rows.</p>
  
  <p>Here some sample date:</p>
  
  <pre><code>a &lt;- data.frame(1.3, 1.4, 1.5, 1.6, 1.7, 1.8)
  Y1 &lt;- runif(100, 5.0, 17.5)
  Y2 &lt;- runif(100, 4.0, 27.5)
  Y3 &lt;- runif(100, 3.0, 14.5)
  Y4 &lt;- runif(100, 2.0, 12.5)
  Y5 &lt;- runif(100, 5.0, 17.5)
  X &lt;- runif(100, 5.0, 7.5)
  df1 &lt;- data.frame(X, Y1, Y2, Y3, Y4, Y5)
  </code></pre>
  
  <p>1) The calculation with the values should look like: </p>
  
  <pre><code># For the first column: (x-a[1,1])^2
  # For the second column: (x-a[1,2])^2
  # For the third column: (x-a[1,3])^2
  #And so on...
  </code></pre>
  
  <p>"x" stands for values from the column. The results (1 per row, so 100 in total) should be stored in a data frame. </p>
  
  <p>2) If possible, the mean value of the results per column should be calculated as well, maybe combined with the code in step 1.</p>
  
  <p>I ran this code but it did not work out. It does not create 100 values per column and the calculation is also wrong.</p>
  
  <pre><code>result1 &lt;- as.data.frame(sapply(df1[1:6], function(x) mean((x-a[1:6])^2)))
  </code></pre>
  
  <p>Can anyone help me with this? I tried to fix it myself, but I still don't see the what is wrong.</p>