I am not sure what the question is. The code runs as it should:
P=matrix(c(0.6,0.2,0.2,0.4,0.3,0.3,.2,.2,.6),nrow=3,byrow=T)
n=100000
x=numeric(n)
pi0=c(0.5,0.2,0.3)
state=sample(1:3,1,prob=pi0)
x[1]=state
x[1]
#> [1] 3
for (i in 2:n) {
state=sample(1:3,1,prob=P[state,])
x[i]=state
}
table(x)
#> x
#> 1 2 3
#> 40486 22262 37252
Created on 2020-03-17 by the reprex package (v0.2.1)
Is this a home work assignment?