Use by = each row for data table
<p>I have a data table and I am trying to create a new variable that is a function of all the other columns. A simplified example would be if I simply wanted to sum or take an average across all the rows. For example: </p>

<pre><code>dt &lt;- data.table(a = 1:9, b = seq(10,90,10), c = seq(11:19), d = seq(100, 900, 100))
</code></pre>

<p>I want to create a vector/column that is simply the average of all the columns. The syntax that I think of would look something like this: </p>

<pre><code>dt[, average := mean(.SD)]
</code></pre>

<p>However, this sums the whole thing. I know I can also do: </p>

<pre><code>dt[, average := lapply(.SD, mean)] 
</code></pre>

<p>But this gives a single row result. I'm essentially looking for the equivalent of:</p>

<pre><code>dt[, average := lapply(.SD, mean), by = all]
</code></pre>

<p>such that it simply calculates this for all the rows, without having to create an "id" column and doing all of my calculating by that column. Is this possible? </p>