title: Create new column based on relative size of values in other columns, data.table
body: <p>I have a table with two columns of values between 0 and 1. For example:</p>
  <pre><code>set.seed(123)
  table &lt;- data.table(value1 = runif(10),
                      value2 = runif(10))
  table
  
     value1     value2
   0.2875775 0.95683335
   0.7883051 0.45333416
   0.4089769 0.67757064
   0.8830174 0.57263340
   0.9404673 0.10292468
   0.0455565 0.89982497
   0.5281055 0.24608773
   0.8924190 0.04205953
   0.5514350 0.32792072
   0.4566147 0.95450365
  </code></pre>
  <p>I would like to use data.table to make a new binary column, assigning 1 to the <code>x</code> rows with the largest difference between value2 and value1.  I can make a &quot;difference&quot; column like this:</p>
  <pre><code>table[,difference:=value1-value2]
  </code></pre>
  <p>And I can find the <code>x</code> largest differences using <code>order</code> and <code>tail</code> e.g. if <code>x</code> is 5:</p>
  <pre><code>x&lt;-5
  table[order(difference), tail(.SD, x)]
  </code></pre>
  <p>But I haven't been able to figure out a way to combine these with something like <code>ifelse</code> or <code>case_when</code> to assign 1 to the <code>x</code> largest differences and 0 to the rest.</p>