了解功能的输出

发布于 2025-02-01 11:19:32 字数 235 浏览 2 评论 0原文

试图了解“交易”的价值是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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蓝海 2025-02-08 11:19:32

这在评论中得到了回答,但是我想添加此细分,因为它可以帮助我可视化每个步骤。

  1. mapply(函数(x,y)min(x,y)):向向量x和y中的每个项目映射min(x,y),因此功能正在这样做:< /p>

     最低(10,12)
    
    最小(4,5)
    
    最小(7,2)
    
    最小(10,6)
    
    最小(12,14)
     

    和输出=(10,4,2,6,12)

  1. sum(mapply(...)):“看到”上面的输出并计算10+4+2+6+12 = 34

This is answered in the comments, but I wanted to add this breakdown since it helps me to visualize each step.

  1. 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:

    min(10,12)
    
    min(4,5)
    
    min(7,2)
    
    min(10,6)
    
    min(12,14)
    

    and outputs = (10, 4, 2, 6, 12)

  1. sum(mapply(...)): Which "sees" the output above and computes 10+4+2+6+12 = 34
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文