限制相减结果的下限 [英] Constrain lower limit of the result of a subtraction
本文介绍了限制相减结果的下限的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想从标量中减去向量中的值.但是,如果结果小于零,我想将结果设置为零.
I want to subtract the values in a vector from a scalar. However, if the result is lower than zero I want to set the result to zero.
我尝试使用 max
,但是它没有给我预期的结果
I have tried using max
, but it doesn't give me the expected result
s
# [1] 750.0 975.0 1125.0 1237.5 1312.5 1400.0
max(1050 - s, 0)
# [1] 300
我希望结果为 c(300,150,0,0,0,0)
推荐答案
我建议 pmax
:
pmax(1050 - s, 0)
# [1] 300 75 0 0 0 0
这篇关于限制相减结果的下限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文