Better way of adding data.frame columns by referring to indeces
<p>This question seems basic but I have not been able to find an answer to it.</p>

<p>I want to add columns of a <code>data.frame</code> together by just referring to their indeces.</p>

<p>Suppose I want to add columns 1,2, and 4.</p>

<pre><code>df &lt;- data.frame(
  a=rep(1, 5),
  b=rep(2, 5),
  c=rep(3, 5),
  d=rep(4, 5)
)
</code></pre>

<p>I know that explicitly referring to the column names I can do</p>

<pre><code>&gt; df$a + df$b + df$d
[1] 7 7 7 7 7
</code></pre>

<p>And referring to indeces I can do</p>

<pre><code>&gt; df[1] + df[2] + df[4]
  a
1 7
2 7
3 7
4 7
5 7
</code></pre>

<p>However, the index option above requires me to write out the name of the <code>data.frame</code> for every column I want to add.</p>

<p>Is there a way to add these columns together while just referring to the indeces and the <code>data.frame</code> once?</p>