I have the following vector
[1] 1 3 5 1 1 4 3 4 5 1 1 2 2 3 1 1 2 4 3 4 4 4 1 1 1
generated by the following piece of code
set.seed(5)
my.matrix = round(matrix(data = runif(n = 25, min = 0, max = 5), ncol = 5),0)
my.vec = as.vector(my.matrix)
I know that I can compute the frequency with which every number was sampled and store it in a data frame with
freq = as.data.frame(table(my.vec))
Suppose now that every number in this vector is a state S.
How can I calculate the number of times all possible transitions from one state to the following state happened?
For instance, how many times S went from 1 to 1, and from 1 to 2, from 1 to 3, and so on.
I am trying to calculate a transition matrix, but the only idea I had so far to carry out this task was to use a for loop with 16 different if statements. This seems like not the optimal path to take.
PS.: As it can be inferred from the code above, I'm coding in R.
Aucun commentaire:
Enregistrer un commentaire