title: Why does my loop in R only saves the last iteration?
body: <p>So I am currently working in this loop, it is the beggining of a Monte Carlo Simulation code: </p>
  
  <pre class="lang-r prettyprint-override"><code>Monacosim &lt;- function(price,strike,daystoexpire, riskfreerate, volatility) {   
  fraccofayear &lt;- daystoexpire/252 
  x = 1
  trump = 1 
  matrix1 &lt;-vector("list", 100000)   
  while (x &lt;= 1) {  # *this matrix is to "store" the 10k random digits*
    print(x) 
    x &lt;- x + 1
    while (trump &lt;= 9999) {
      trump &lt;- trump + 1 
      matrix1[] &lt;- data.frame((price*exp((riskfreerate-.5*volatility^2)*fraccofayear+volatility*sqrt(fraccofayear)*rnorm(1)))) 
  # *this is the formula that generates the random option prices, and this is the line where I am "saving" all the generated data into the matrix* 
    }
    trump = 0 
    print(matrix1)   
  }
  </code></pre>
  
  <p>I need to do 52 periods of 10k interations each. The variable X is the counter for the 52 periods (in this case I am just running one for experimental purposes) and the "Trump" indicator is the one in charge to repeat the iterations 10k times. I already tested and the 10k DIFFERENT iterations are being made, as well as the times on periods I want (ex. if I put 52 it does the process 52 times), but just the last number is saved and printed 10k times. I know there must be something I need to insert inside the brackets in matrix1[] but in this case "i" won't cut it. What am I doing wrong?</p>