Ok, I assumed you wanted to compute a matrix M for all states such that M[i][j]= transition from state i to state j / number of transition from state i but from what you just answered it looks like you want to compute a matrix M for a set of states S such that: M[S_i][S_j]= transition from state S_i to state S_j / number of transition from state S_i where S_i is in the set S ? 2017-08-28 17:50 GMT+02:00 niharika singhal <niharikasinghal1990 at gmail.com>:> Sorry I have typed wrong value for output > (2/6, 1/6, 3/6) > > On Mon, Aug 28, 2017 at 5:49 PM, niharika singhal < > niharikasinghal1990 at gmail.com> wrote: > >> Hi Elie, >> >> Thanks for the mail >> >> If the number of states will change then the data frame will also have 4 >> as one of the value >> Also I am only intersted in i and (i+1) (linear search for each state) >> >> in the above data frame for state1 ={1} the output will like >> (1/6, 1/6, 3/6) >> >> Your code is giving me some error but I will see if I can remove and use >> it. >> >> Regards >> Niharika >> >> On Mon, Aug 28, 2017 at 5:31 PM, Elie Canonici Merle < >> elie.canonicimerle at 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 < >>> niharikasinghal1990 at 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 at 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/posti >>>> ng-guide.html >>>> and provide commented, minimal, self-contained, reproducible code. >>>> >>> >>> >> >[[alternative HTML version deleted]]