了解功能的输出
试图了解“交易”的价值是34个
available <- c(10,4,7,10,12)
desired <- c(12,5,2,6,14)
traded <- sum(mapply(function(x,y) min(x,y), available, desired))
正确的价值是34。不确定为什么是这种情况。我认为该值将为6,因为每个向量的最小值(4和2)汇总= 6
Trying to understand how the value of "traded" is 34
available <- c(10,4,7,10,12)
desired <- c(12,5,2,6,14)
traded <- sum(mapply(function(x,y) min(x,y), available, desired))
Correct value for traded is 34. Just not sure why this is the case. I thought the value would be 6 as the minimum values from each vector (4 and 2) summed together =6
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这在评论中得到了回答,但是我想添加此细分,因为它可以帮助我可视化每个步骤。
mapply(函数(x,y)min(x,y)):向向量x和y中的每个项目映射min(x,y),因此功能正在这样做:< /p>
和输出=(10,4,2,6,12)
This is answered in the comments, but I wanted to add this breakdown since it helps me to visualize each step.
mapply(function(x,y) min(x,y)): Maps min(x,y) to each item in vectors x and y , so the function is doing this:
and outputs = (10, 4, 2, 6, 12)