multiplying all elements of a vector in R

后端 未结 2 1521
北荒
北荒 2020-11-29 08:20

I want a function to return the product of all the values in a vector, like sum but with multiplication instead of addition. I expected this to exist already, b

相关标签:
2条回答
  • 2020-11-29 08:36

    If your data is all greater than zero this is a safer solution that will not cause compute overflows:

    exp(sum(log(x)))
    
    0 讨论(0)
  • 2020-11-29 08:40

    You want prod:

    R> prod(1:3)
    [1] 6
    
    0 讨论(0)
提交回复
热议问题