title: Create column with (for each row) maximum value in a subset based on values in that row
body: <p>I have a data frame <code>df</code> with three columns. Two input columns <code>input1</code> and <code>input2</code> and an <code>output</code> column.</p>
  <p>I want to create a new column with the maximum value in <code>output</code> within a subset of <code>df</code> which is based on all rows in which <code>input1</code> and <code>input2</code> are below or equal to the respective input values in the respective row.</p>
  <p>I managed to do that in a for loop easily:</p>
  <pre><code>output &lt;- c(1:10)
  input1 &lt;- c(5,5,10,10,7,7,20,9,12,18)
  input2 &lt;- c(8,6,16,16,8,20,21,12,30,21)
  
  df &lt;- as.data.frame(cbind(output, input1, input2))
    
    
    for (i in 1:nrow(df)){
      df[i,&quot;max&quot;] &lt;- max(df$output[df$input1 &lt;= df$input1[i] &amp;
                                           df$input2 &lt;= df$input2[i]])
    }
  </code></pre>
  <p>However, it is not feasible with my original data with up to 1.000.000 observations.</p>
  <p>Is there any option with apply or within data.table to speed up this process?</p>