【问题标题】:Better way to find a minimum value that fits a condition?找到适合条件的最小值的更好方法?
【发布时间】:2012-03-24 15:32:11
【问题描述】:

我无法摆脱我错过了一些明显的东西的感觉。是否有更清晰或更惯用的方式来执行以下函数的功能?

closest.preceding <- function(candidates, limit) {
    # return the value in candidates that is closest to but less than the limit
    return(limit - min(limit-candidates[candidates < limit]))
}

感谢您的任何见解。

【问题讨论】:

    标签: r minimum


    【解决方案1】:

    你可以这样做:

    max(candidates[candidates<limit])
    

    首先过滤掉那些(严格)小于限制的候选者,然后取其中的最大值(最接近)。

    我相信还有其他方法。

    【讨论】:

    • 我很确定这就是我发烧的大脑试图让我做的事情。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-28
    • 1970-01-01
    • 1970-01-01
    • 2016-12-17
    • 1970-01-01
    • 2023-03-28
    相关资源
    最近更新 更多