Home > Software engineering >  Extract the first row that meets a criteria in R
Extract the first row that meets a criteria in R

Time:09-24

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

CodePudding user response:

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

CodePudding user response:

We can also use match -

with(rating_curve, time_h[match(TRUE, depth_m > 0.61)])
#[1] 0.05
  •  Tags:  
  • r
  • Related