找到适合条件的最小值的更好方法是什么?

Pat*_*ick 3 r minimum

我无法理解我错过了一些明显的东西.是否有更清晰或更惯用的方式来执行以下功能?

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]))
}
Run Code Online (Sandbox Code Playgroud)

感谢您的任何见解.

mat*_*fee 5

你可以这样做:

max(candidates[candidates<limit])
Run Code Online (Sandbox Code Playgroud)

哪个首先筛选出(严格地)小于限制的候选者,然后取出那些(最接近的)的最大值.

我相信还有其他方法.