title: R: Calculate Markov transition matrix when sequences are of unequal lengths
body: <p>I have several stand-alone observed sequences of different length called <code>movs</code> and would like to calculate a general transition matrix for movs. There are two problems: </p>
  
  <ol>
  <li>Not all states appear in each sequence. </li>
  <li><p>I cannot concatenate them.</p>
  
  <p>df&lt;-data.frame(activityID=rep(1:4,c(10,5,4,1))),movs=sample(c("A","B","C"),20,replace=T))</p></li>
  </ol>
  
  <p>I've looked at the package <code>markovchain</code> which has a function <code>markovchainfit</code> and an argument <code>possibleStates</code> which allows me to pre-define all the states that will exist in the sequence even if they do not appear in all of them.
  <a href="https://rdrr.io/a/cran/markovchain/man/markovchainFit.html" rel="nofollow noreferrer">https://rdrr.io/a/cran/markovchain/man/markovchainFit.html</a></p>
  
  <p>But because the sequences aren't in rows and because they are of different lengths  I convert df to a list:</p>
  
  <pre><code>d&lt;-split(df$movs, df$activityID) 
  </code></pre>
  
  <p>Then run <code>markovfitlist</code> but it doesn't have the <code>possibleStates</code> argument. </p>
  
  <pre><code>markovchain::markovchainListFit(data = d,byrow = F)
  </code></pre>
  
  <p>So now I have a transition matrix for each sequence - <strong>How do I combine them all to make 1 transition matrix that includes all the states?</strong> </p>