title: Replacing rows in a matrix in a loop in R
body: <p>I am trying to write a simple Markov model in R. To generate the Markov trace I created a empty matrix with ncol=number of states, and nrow=number of cycles. I am writing a function using a loop to replace each row of the matrix with each cycle, but the loop is failing me. </p>
  
  <pre><code>Rounds&lt;-function(cycles,TransMatrix){
    for (i in 1:nrow(MarkovTrace)){
      MarkovTrace[i+1,]&lt;-as.vector(MarkovTrace[i,]%*%TransMatrix)
    }
  }
  </code></pre>
  
  <p>Any thoughts on why this is? 
  I am able to do this manually outside of the loop, though. </p>
  
  <pre><code>MarkovTrace[2,]&lt;-as.vector(MarkovTrace[1,]%*%TransMatrix)
  MarkovTrace[3,]&lt;-as.vector(MarkovTrace[2,]%*%TransMatrix)
  </code></pre>
  
  <p>Many thanks!</p>