Extract the first row that meets a criteria in R

I need to extract the first time that the depth a value greater than 0.61m. I've seen several complex answers to this type of question, but I need a simple and mostly quick answer. thanks for the help

Thanks for the help.

rating_curve = data.frame(time_h = c(0.01, 0.02, 0.03, 0.04, 0.05, 0.06),
                      depth_m = c(0, 0.2, 0.3, 0.5, 0.7, 0.62))

Correct answer

[1] 0.05

Upvotes: 0

Views: 61

Answers (2)

Ronak Shah
Ronak Shah

Reputation: 389355

We can also use match -

with(rating_curve, time_h[match(TRUE, depth_m > 0.61)])
#[1] 0.05

Upvotes: 0

akrun
akrun

Reputation: 887991

We can use use > to create a logical vector, subset the 'time_h' using that, extract the first element ([1])

with(rating_curve, time_h[depth_m > 0.61][1])
[1] 0.05

Upvotes: 1

Related Questions