title: Recursive for loop in R
body: <p>I am struggling to code this recursive program and wondering if anyone could help.</p>
  
  <p>I want to code this recursive equation:</p>
  
  <pre><code>for k=1,2,...
  
  beta(k)=k-sum_(i=0)^(k-1)Kchoosei*beta(i)*exp(-i(k-i))
  </code></pre>
  
  <p>I've done it the manual way in R but would like to put it in a function.</p>
  
  <pre><code>beta0&lt;-0
  
  beta1&lt;-1-choose(1,0)*beta0*exp(-0*lambdaL*(1-0))
  beta2&lt;-2-choose(2,0)*beta0*exp(-0*lambdaL*(2-0))-choose(2,1)*beta1*exp(-1*lambdaL*(2-1))
  beta3&lt;-3-choose(3,0)*beta0*exp(-0*lambdaL*(3-0))-choose(3,1)*beta1*exp(-1*lambdaL*(3-1))-choose(3,2)*beta2*exp(-2*lambdaL*(3-2))
  beta4&lt;-4-choose(4,0)*beta0*exp(-0*lambdaL*(4-0))-choose(4,1)*beta1*exp(-1*lambdaL*(4-1))-choose(4,2)*beta2*exp(-2*lambdaL*(4-2))-choose(4,3)*beta3*exp(-3*lambdaL*(4-3))
  </code></pre>