title: Simulating a Markov Chain in R and Sequence Search
body: <p>So I am working on simulating a Markov Chain with R in which the states are Sunny (S), cloudy (C) and rainy (R) and am looking to figure out the probability that a sunny day is followed by two consecutive cloudy days.  </p>
  
  <p>Here is what I have so far:</p>
  
  <pre><code>    P = matrix(c(0.7, 0.3, 0.2, 0.2, 0.5, 0.6, 0.1, 0.2, 0.2), 3)
      print(P)
      x = c("S", "C", "R") 
      n = 10000
      states = character(n+100)
      states[1] = "C"
  
      for (i in 2:(n+100)){
      if (states[i-1] == "S") {cond.prob = P[1,]}
      else if (states[i-1] == "C") {cond.prob = P[2,]}
      else {cond.prob = P[3,]}
      states[i]=sample(x, 1, prob = cond.prob )
      }
  
      print(states[1:100])
      states = states[-(1:100)]  
      head(states)
      tail(states)
      states[1:200]
  </code></pre>
  
  <p>At the end of this I am left with a sequence of states.  I am looking to divide this sequence into groups of three states (For the three days in the chain) and then count the number of those three set states that are equal to SCC. </p>
  
  <p>I am running a blank on how I would go about doing this and any help would be greatly appreciated!!</p>