Take the 2-minute tour ×


veteran$prognostic_indicator <- 0
veteran$prognostic_indicator[veteran$karno<50] <- 1
model <- coxph(Surv(time,status)~age+prognostic_indicator,data=veteran)
object <- obsSensSCC(model, which = "prognostic_indicator", g0 = seq(1,10,0.01),p0 = c(0.05,0.1,0.2,0.3,0.4), p1 = seq(0, 1, 0.05), logHaz = FALSE, method = "approx")

I can extract the vector:


Which is ordered by descending values. I want to extract the "name" of the number which is closest to 1, but above it. In that case I want to extract the name "2.69" or position 170 since the corresponding number is 1.0001292. The number at position 2.70 is 0.9968844 and thus too low.

How do I extract the position (or name) in a vector of descending values where the number is nearest the value 1.0, but above?

share|improve this question
I think you're explaining the background a lot (which is not necessary at all) and not focussing on the title of your question. It'd be great if you edit your question to provide a small example (with a matrix or data.frame) showing the different columns and values properly and explaining what you mean by closest value (of which column etc...) –  Arun Jan 22 '13 at 21:17
Yes I agree completely. Got a bit caught up in it. Tried to revise it. –  Kjetil Loland Jan 22 '13 at 21:53
add comment

2 Answers

up vote 1 down vote accepted

If you create a new vector with that value, then identify the first element that satisfies the condition, then move one back in the sequence.

obj <- object$lcl[21,1,1:901] 
obj[which(obj< 1)[1] -1]

#    2.69 

The other way would be to work on the reversed vector. Then you do not need to backtrack:

> rev(obj)[which(rev(obj) > 1)[1] ]
share|improve this answer
Thank you! That works like a charm! –  Kjetil Loland Jan 23 '13 at 7:29
add comment

Here's another way in addition to DWin's cleaner method.

which.min(subset(object$lcl[21,1,1:901], object$lcl[21,1,1:901] > 1) - 1)
share|improve this answer
Thank you! Works perfectly as well! –  Kjetil Loland Jan 23 '13 at 7:29
add comment

Your Answer


