All of this can be done without for loops. Use head(..., -1), tail(..., -1) to get the pre and post states.
Use factor or pmin to recode them as necessary Use table(pre, post) to get the transition counts. Use prop.table(table_of_counts,1) to get the probabilities. HTH, Chuck > On Aug 28, 2017, at 8:31 AM, Elie Canonici Merle > <elie.canonicime...@gmail.com> wrote: > > Hi, > > I think you overthought this one a little bit, I don't know if this is the > kind of code you are expecting but I came up with something like that: > > generate_transition_matrix <- function(data, n_states) { > > #To be sure I imagine you should check n_states is right at this point > > transitions <- matrix(0, n_states, n_states) > > #we could improve a little bit here because at step N+1 source is dest > from step N > #but it would not be as readable > for (k in 1:(length(data) - 1)) { > source_state <- data[k] > dest_state <- data[k+1] > transitions[source_state, dest_state] <- transitions[source_state, > dest_state] + 1 > } > > for (k in 1:n_states) > transitions[k,] <- transitions[k,] / sum(transitions[k,]) > transitions > } > > checkdf=data.frame(clusterNum=c(3,2,3,1,1,3,4,3,2,1,1,3,2,1,3,2)) > no_of_state=4 > > transition_matrix= generate_transition_matrix(checkdf$clusterNum, > no_of_state) > transition_matrix > > > > 2017-08-28 16:37 GMT+02:00 niharika singhal <niharikasinghal1...@gmail.com>: > >> Hello, >> >> I am trying to implement a formula >> >> aij= transition from state S_i to S_j/no of transition at state S_i >> >> >> >> Code I have written is working with three state {1,2,3 }, but if the number >> of states become={1,2,3,4,......n} then the code will not work, so can some >> help me with this. >> >> For and some rows of my data frame look like >> >> checkdf=data.frame(clusterNum=c(3,2,3,1,1,3,4,3,2,1,1,3,2,1,3,2) >> no_of_state=3 >> transition_matrix=matrix(NA,nrow=no_of_state, ncol=no_of_state) >> for(k in 1: no_of_state) >> { >> count1=0 >> count2=0 >> count3=0 >> #For last point no transition takes place >> for(j in 1: (nrow(checkdf)-1)) >> { >> >> if(checkdf$clusterNum[j]==k) >> { >> if(checkdf$clusterNum[j+1]==1){ >> count1=count1+1 >> } >> else if(checkdf$clusterNum[j+1]==2){ >> count2=count2+1 >> } >> else { >> count3=count3+1 >> } >> } >> } >> >> no_of_points=(count1+count2+count3) >> s1=count1/no_of_points >> s2=count2/no_of_points >> s3=count3/no_of_points >> transition_matrix[k,]=c(s1, s2, s3) >> >> } >> >> I know the code is not written nicely and I want to improve it. >> >> Thanks in advance >> Niharika >> >> [[alternative HTML version deleted]] >> >> ______________________________________________ >> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see >> https://stat.ethz.ch/mailman/listinfo/r-help >> PLEASE do read the posting guide http://www.R-project.org/ >> posting-guide.html >> and provide commented, minimal, self-contained, reproducible code. >> > > [[alternative HTML version deleted]] > ______________________________________________ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.