Constrain lower limit of the result of a subtraction

吃可爱长大的小学妹 提交于 2020-08-08 06:59:04

问题


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.

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

I expect result to be c(300, 150, 0, 0, 0, 0)


回答1:


I suggest pmax:

pmax(1050 - s, 0)
# [1] 300  75   0   0   0   0


来源:https://stackoverflow.com/questions/3438049/constrain-lower-limit-of-the-result-of-a-subtraction

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!