[R] list inside a for loop

Nicolas Gutierrez nicolasg at uw.edu
Sat Jan 22 22:41:42 CET 2011


Hi All,

Trying to figure out the way to create a vector of values inside a loop

I have a matrix pop:

   Id yloc xloc weight
1
2
.

and spatial grid grid.dens with "coordinates" (x=xloc,y=yloc)

I want to create two more vectors: Food and Consumed inside the loop and 
add those to pop so:

   Id yloc xloc weight Food Consumed
1
2
.

I'm trying something like this but not getting what I want.

nagent=100
Food=list()
Consumed=list()

ind=function(pop, grid.dens){

for (i in 1:nagent){

Food[[i]]=grid.dens[pop$yloc[i],pop$xloc[i]]

Consumed[[i]]=pop$weight[i]*0.8*(Food[[i]]/(0.2+Food[[i]]))

Food[[i]]=Food[[i]]-Consumed[[i]]

}

Any thoughts?

THANKS!!!

Nico



More information about the R-help mailing list