title: R get correct index using which() condition
body: <p>I have to find an observation satisfying some criteria and then merge this indices with an other dataset. So I don't need the index of the observations satisfying the condition, but the index that refers to all the observations.</p>
  
  <p>For instance, I want to find the <code>max(x1)</code> given that <code>x2&gt;20</code> and then use this index in another dataset later. I need the right index, in other words:</p>
  
  <pre><code>dat &lt;- data.frame(name= c("A","B","C","D"),
             x1= c(1,2,3,4),
             x2= c(10,20,30,40))
  dat$name[which.max(dat$x1[dat$x2&gt;20])]
  [1] B
  </code></pre>
  
  <p>I want to get </p>
  
  <pre><code>[1] D
  </code></pre>
  
  <p>i.e. an index of 4, not 2.</p>