title: How to apply operation and sum over columns in R?
body: <p>I want to apply some operations to the values in a number of columns, and then sum the results of each row across columns.  I can do this using:</p>
  
  <pre><code>x &lt;- data.frame(sample=1:3, a=4:6, b=7:9)
  x$a2 &lt;- x$a^2
  x$b2 &lt;- x$b^2
  x$result &lt;- x$a2 + x$b2
  </code></pre>
  
  <p>but this will become arduous with many columns, and I'm wondering if anyone can suggest a simpler way.  Note that the dataframe contains other columns that I do not want to include in the calculation (in this example, column <code>sample</code> is not to be included).</p>
  
  <p>Many thanks!</p>