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.
For instance, I want to find the max(x1) given that x2>20 and then use this index in another dataset later. I need the right index, in other words:
dat <- 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>20])]
[1] B
I want to get
[1] D
i.e. an index of 4, not 2.