The following source code is from a book. Comments are written by me to understand the code better.
#============================================================
Names of the states don't really need to have any statistical meaning as long as they are different. So, while simulating transitions between states, it's perfectly fine to choose states <- 1:length(init)
or any other names for them. Ultimately, though, for practical purposes we often have in mind some labels in mind, such as -1, 0, ..., n, as in your example. You can provide those names as the labels
parameter and then labels[simlist]
will rename 1:length(init)
to labels
, element by element. I.e., if initially we had c(1, 2, 3)
and you provided labels
as c(5, 10, 12)
, then the output will be in terms of the latter vector. For instance,
(states <- sample(1:3, 10, replace = TRUE))
# [1] 1 3 3 2 2 1 2 1 3 3
labels <- c(5, 10, 12)
labels[states]
# [1] 5 12 12 10 10 5 10 5 12 12