Ncount4<-function(seq,w){ #w=length of word tcount<-array(rep(0,4^w), rep(4,w)) # array[4x4x4x4] to hold word counts, elements set to zero ncount<-array(rep(0,4^w), rep(4,w)) # array[4x4x4x4] holds number of sequences with one or # more of each k-word N<-length(seq[1,]) #Length of each sequence M<-length(seq[,1]) #Number of sequences ####################################################### #Count total number of word occurrences for(j in 1:M){ #looping over sequences jcount<-array(rep(0,4^w), rep(4,w)) #array to hold word counts for sequence j for(k in 1:(N-w+1)){ #looping over positions jcount[seq[j,k],seq[j,k+1],seq[j,k+2],seq[j,k+3]]<- jcount[seq[j,k],seq[j,k+1],seq[j,k+2],seq[j,k+3]]+1 #adds 1 if word at k,k+1,k+2,k+3 appears in sequence j } tcount<-tcount+jcount #Add contribution of j to total ####################################################### #Plug-in: add 1 to ncount if word occurs >= once in j for(k in 1:4){ for(l in 1:4){ for(m in 1:4){ for(n in 1:4){ if(jcount[k,l,m,n]!=0){ ncount[k,l,m,n]<-ncount[k,l,m,n]+1} } } } } ####################################################### } return(tcount,ncount) }