title: R: Incorrect number of probabilities error when running Markov Chain simulation
body: <p>This is from chapter 9 of "Analyzing Baseball Data with R," attempting to simulate runs scored in a half inning;</p>
  
  <pre><code>simulate&lt;-function(P,R,start=1){
  s&lt;-start; path&lt;-NULL; runs&lt;-0
  while(s&lt;25){
  s.new&lt;-sample(1:25, 1, prob=P[s, ])
  path&lt;-c(path, s.new)
  runs&lt;-runs + R[s, s.new]
  s&lt;-s.new
  }
  runs
  }
  </code></pre>
  
  <p>this is pretty much identical to the code in the book. When I attempt to run it with the following code, I get the following error;</p>
  
  <blockquote>
    <p>RUNS&lt;-replicate(10000,simulate(tmatrix,R))</p>
  </blockquote>
  
  <p><strong>Error in sample.int(length(x), size, replace, prob) : 
    incorrect number of probabilities</strong> </p>
  
  <p>Clicking on "rerun with debug" in Rstudio points out the following line;</p>
  
  <pre><code>s.new&lt;-sample(1:25, 1, prob=P[s, ])
  </code></pre>
  
  <p>Which is the only thing that makes sense here since it has an issue with the number of probabilities. Any ideas on how to alter this?</p>