title: Loop based on identification number
body: <p>I would like to generate all the transition matrixes based on id. How can I loop my data to calculate the transitions based on id? Example below is for id 1, I would like to have all matrixes till id 5</p>
  <p>Example of code that incorporates markov transition calculations:</p>
  <pre><code>  Ep&lt;-as.matrix(Activities[1:1,2:4])
      
      
      trans.matrix &lt;- function(X, prob=T)
      {
        tt &lt;- table( c(X[,-ncol(X)]), c(X[,-1]) )
        if(prob) tt &lt;- tt / rowSums(tt)
        as.matrix(tt)
      }
      
      Ep&lt;-trans.matrix(as.matrix(Ep))
  </code></pre>
  <p>Data structure:</p>
  <p><a href="https://i.stack.imgur.com/QFdHb.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/QFdHb.png" alt="enter image description here" /></a></p>
  <p>Sample data:</p>
  <pre><code>structure(list(id = c(1, 2, 3, 4, 5), a = c(0, 2, 2, 3, 1), b = c(1, 
  3, 3, 2, 2), c = c(1, 4, 4, 3, 3), d = c(0, 5, 3, 2, 4)), class = c(&quot;spec_tbl_df&quot;, 
  &quot;tbl_df&quot;, &quot;tbl&quot;, &quot;data.frame&quot;), row.names = c(NA, -5L), spec = structure(list(
      cols = list(id = structure(list(), class = c(&quot;collector_double&quot;, 
      &quot;collector&quot;)), a = structure(list(), class = c(&quot;collector_double&quot;, 
      &quot;collector&quot;)), b = structure(list(), class = c(&quot;collector_double&quot;, 
      &quot;collector&quot;)), c = structure(list(), class = c(&quot;collector_double&quot;, 
      &quot;collector&quot;)), d = structure(list(), class = c(&quot;collector_double&quot;, 
      &quot;collector&quot;))), default = structure(list(), class = c(&quot;collector_guess&quot;, 
      &quot;collector&quot;)), skip = 1L), class = &quot;col_spec&quot;))
  </code></pre>