Search code examples
rsumsequencecumulative-sum

Cumulative sums in R


I want to compute the following term in R, but without using loops ("for" cycles):

enter image description here

So far, I already calculated it manually and tried to use sequence functions for both index variables i and j and integrated them in a cumsum function and summed up the returning vector.

i <- seq(1:10)
j <- seq(1:5)

sum(cumsum((i^5)/(10+j^i)))

However, the results do not match with my manual calculation, so this seems to be a wrong approach. Could anybody help me out on this?


Solution

  • Try outer

    > sum(outer(1:10, 1:5, FUN = function(i,j) i^5/(10+j^i)))
    [1] 20845.76