I am trying to use R to model grids of agents that change their decisions based on the decisions of other agents in their direct proximity. Basically, each agent looks to the other agents around him on the grid, and might change its behaviour based on the the actions around him. I have included some sample dinky-toy code per below to show (one iteration) of such dynamic.
I am wondering whether
there is an elegant manner to address the boundaries of the grid (currently the t+1, i+1 code doesn't work on the edges),
or whether there are other approaches that use the "spatial" dimension of the matrix / use graph-based approaches to simulate these kind of models?
NCols=10
NRows=10
df=round(matrix(runif(NCols*NRows), ncol=NCols),0); df
t=1;i=1
for(i in 1:(nrow(df)-1)){
for(t in 1:(ncol(df)-1)){
prox=sum(df[t+1,i]+df[t+1,i-1]+df[t+1,i+1]+df[t,i]+df[t,i-1]+df[t,i+1]+df[t-1,i]+df[t-1,i-1]+df[t-1,i+1])
if(prox<=3) {df[t,i]=0} else {df[t,i]=1}
}
}
df