title: How to retrieve column for row-wise maximum value in an R data.table?
body: <p>I have the following R data.table:</p>
  
  <pre><code>library(data.table)
  iris = as.data.table(iris)
  &gt; iris
      Sepal.Length Sepal.Width Petal.Length Petal.Width    Species
  1            5.1         3.5          1.4         0.2     setosa
  2            4.9         3.0          1.4         0.2     setosa
  3            4.7         3.2          1.3         0.2     setosa
  4            4.6         3.1          1.5         0.2     setosa
  5            5.0         3.6          1.4         0.2     setosa
  6            5.4         3.9          1.7         0.4     setosa
  7            4.6         3.4          1.4         0.3     setosa
  8            5.0         3.4          1.5         0.2     setosa
  ...
  </code></pre>
  
  <p>Let's say I wanted to find the row-wise maximum value by each row, only for the subset of data.table columns: <code>Sepal.Length</code>, <code>Sepal.Width</code>, <code>Petal.Length</code>, <code>Petal.Width</code></p>
  
  <p>I would use the following code:</p>
  
  <pre><code>iris[, maximum_element :=max(Sepal.Length, Sepal.Width, Petal.Length, Petal.Width), by=1:nrow(iris)]
  </code></pre>
  
  <p>Which outputs</p>
  
  <pre><code>     Sepal.Length Sepal.Width Petal.Length Petal.Width   Species     maximum_element
    1:          5.1         3.5          1.4         0.2    setosa               5.1
    2:          4.9         3.0          1.4         0.2    setosa               4.9
    3:          4.7         3.2          1.3         0.2    setosa               4.7
    4:          4.6         3.1          1.5         0.2    setosa               4.6
    5:          5.0         3.6          1.4         0.2    setosa               5.0
  </code></pre>
  
  <p>For my problem, I'm actually not interested in the value, but which column the value came from, i.e. I would like the following output:</p>
  
  <pre><code>     Sepal.Length Sepal.Width Petal.Length Petal.Width   Species maximum_column
        1:          5.1         3.5          1.4         0.2    setosa  Sepal.Length
        2:          4.9         3.0          1.4         0.2    setosa  Sepal.Length
        3:          4.7         3.2          1.3         0.2    setosa  Sepal.Length
        4:          4.6         3.1          1.5         0.2    setosa  Sepal.Length
        5:          5.0         3.6          1.4         0.2    setosa  Sepal.Length
  </code></pre>
  
  <p>(In this case, the max. value each comes from <code>Sepal.Length</code>). </p>
  
  <p>How do I "retrieve" the column name with the maximum value? </p>