How to apply function to each row of data.table
<p>I am trying to calculate net present value (NPV) using <code>library(financial)</code> for each observation from given cashflow in <code>data.table</code> format.  Here is my cashflow:</p>

<pre><code>library(data.table)    
dt &lt;- data.table(id=c(1,2,3,4), Year1=c(NA, 30, 40, NA), Year2=c(20, 30, 20 ,70), Year3=c(60, 40, 0, 10))
</code></pre>

<p>To calculate NPV and update in data.table,</p>

<pre><code>library(financial)
npv &lt;- apply(dt, 1, function(x) cf(na.omit(x[-1]), i = 20)$tab[, 'NPV'])
dt[, NPV:=npv]
</code></pre>

<p>return,</p>

<pre><code>   id Year1 Year2 Year3      NPV
1:  1    NA    20    60 70.00000
2:  2    30    30    40 82.77778
3:  3    40    20     0 56.66667
4:  4    NA    70    10 78.33333
</code></pre>

<p>How can I directly update result using function <code>cf</code> to each row in the <code>data.table</code> ? </p>

<p>FYI: In my real dataset, there are over 50 columns </p>