title: Row maximum in data table
body: <p>I have a dataset of 8,000,000 rows with 100 columns in a data.table where each column is a count. I need to find the maximum count in each row and which column this maximum is in.</p>
  
  <p>I can quickly get which column has the maximum value for each row using </p>
  
  <pre><code>dt &lt;- dt[, maxCol := which.max(.SD), by=pmxid]
  </code></pre>
  
  <p>but trying to get the actual maximum value using</p>
  
  <pre><code>dt &lt;- dt[, nmax := max(.SD), by=pmxid]
  </code></pre>
  
  <p>is incredibly slow. I ran it for nearly 20 mins and only 200,000 row maximums had been calculated. Finding the max column took approx. 2 mins for all 8,000,000 rows.</p>
  
  <p>How come finding the maximum takes so long? Shouldn't it take the same time as <code>which.max()</code> or less?</p>