【问题标题】:Extract the first row that meets a criteria in R提取满足 R 中条件的第一行
【发布时间】:2021-09-24 00:27:43
【问题描述】:

我需要提取第一次那个深度大于0.61m的值。对于此类问题,我已经看到了几个复杂的答案,但我需要一个简单且最快速的答案。感谢您的帮助

感谢您的帮助。

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))

正确答案

[1] 0.05

【问题讨论】:

  • rating_curve$time_h[which(rating_curve$depth_m>0.61)[1]]

标签: r


【解决方案1】:

我们可以使用 > 创建一个逻辑向量,使用它对“time_h”进行子集化,提取第一个元素 ([1])

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

【讨论】:

【解决方案2】:

我们也可以使用match -

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

【讨论】:

    猜你喜欢
    • 2015-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-02
    • 2020-01-31
    • 1970-01-01
    • 2018-12-22
    相关资源
    最近更新 更多