Search code examples
rfor-loopsum

Summation in R quicker method


I have a summation from i=1 to 10 in 10.000/1/12^t = 56.502 In R :

n =10
t = seq(1,n,1);t
g = rep(0,n) 
r = 0.12
for (i in t) {
   g[i] = 10000/((1+r)^t[i])
   GPV = sum(g)
}
GPV

Is there a quicker way other than for loop in R, in order to calculate this summation ?


Solution

  • Try this

    > n <- 10
    
    > r <- 0.12
    
    > sum(10000 / (1 + r)^seq(n))
    [1] 56502.23