title: Select max of absolute value for each numeric column by a grouping column in data.table or dplyr
body: <p>Here is an example of what my data.frame looks like:</p>
  
  <pre><code>opts &lt;- seq(-0.5, 0.5, 0.05)
  df &lt;- data.frame(combo1=sample(opts, 6),
                   combo2=sample(opts, 6),
                   combo3=sample(opts, 6),
                   gene=rep(c("g1", "g2", "g3"), each=2), stringsAsFactors=F)
  
  df
     combo1 combo2 combo3 gene
  1   0.40   0.50  -0.10   g1
  2   0.10  -0.20  -0.35   g1
  3  -0.35  -0.35   0.40   g2
  4   0.00   0.10  -0.30   g2
  5  -0.45  -0.10   0.05   g3
  6  -0.40  -0.40  -0.05   g3
  </code></pre>
  
  <p>For each combo, I want to group by gene and then select the maximum absolute value. I can accomplish this using dplyr:</p>
  
  <pre><code>library(dplyr)
  df_final &lt;- data.frame(row.names=unique(df$gene))
  
  for (combo in colnames(df)[1:3]) {
  
      combo_preds &lt;- df[, c(combo, "gene")]
      colnames(combo_preds) &lt;- c("pred", "gene")
  
      combo_preds %&gt;%
          group_by(gene) %&gt;%
          arrange(desc(abs(pred))) %&gt;%
          slice(1) %&gt;%
          ungroup() -&gt;
          combo_preds
  
      #add to df_final
      class(combo_preds) &lt;- "data.frame"
      df_final[combo_preds$gene, combo] &lt;- combo_preds$pred
  }
  #names rows based on gene
  row.names(df_final) &lt;- unique(df$gene)
  
  df_final
      combo1 combo2 combo3
  g1   0.40   0.50  -0.35
  g2  -0.35  -0.35   0.40
  g3  -0.45  -0.40   0.05
  </code></pre>
  
  <p>Is there a way that I can accomplish the above with data.table or some other more efficient implementation? In reality, I have ~1300 dfs, each with ~14000 genes and ~650 combos. The current implementation takes 2.6 minutes per df so it will take over 2 days.</p>