2012-03-07 47 views
2

我无法摆脱我失去一些明显的东西的感觉。有没有更清晰或更习惯的方式去做以下功能?找到符合条件的最小值的更好方法?

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

感谢您的任何见解。

回答

3

你可以这样做:

max(candidates[candidates<limit]) 

它首先过滤掉那些只是候选人是(严格)小于极限,然后采取那些最大(其中大部分是最接近)。

我敢肯定还有其他方法。

+0

我很确定这是我的狂热大脑试图让我去做的事情。谢谢! – Patrick 2012-03-07 05:14:19