[R] random walk on graph

Christos Hatzis christos at silicoinsights.com
Thu Apr 27 17:44:27 CEST 2006

I did not look at your code in detail, but I see a potential problem in your
inner loop.  I think you intended k to be in 2,3,...N+1.  What you get from
your code is (2:N)+1 according to the operator precedence rules.  You need


-----Original Message-----
From: r-help-bounces at stat.math.ethz.ch
[mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Eric Blabac
Sent: Thursday, April 27, 2006 11:28 AM
To: r-help at stat.math.ethz.ch
Subject: [R] random walk on graph

Hi all,
I'm having issues coding a random walk on a fully connected, undirected
graph G with probability transition matrix P = (p_ij). Here is what I have
so far ...

for(i in 1:n){
	for(m in 1:M){
		x <- as.vector(matrix(rep(0,N+1),nc=N+1))
		x[1] <- i

		for(k in 2:N+1){
			y <- as.vector(matrix(rep(0,n),nc=n))
			r <- runif(1)
			c <- c(0,cumsum(P[x[k-1],]))

			for(j in 1:n){
				y[j] <- (r >= c[j] & r < c[j+1])
			x[k] <- which(y == 1)
		L[i,m] <- x[N+1]

its giving me the error:

Error in "[<-"(`*tmp*`, k, value = integer(0)) :
        nothing to replace with

no matter what I do I cant fix it ... any suggestions ?

Eric Blabac
PhD Student - ASU Dept of Mathematics/Statistics

R-help at stat.math.ethz.ch mailing list
PLEASE do read the posting guide!

More information about the R-help mailing list