[R] return object from loop inside a function

Nicolas Gutierrez nicolasg at uw.edu
Wed Jan 26 03:41:38 CET 2011


Hi All,

I have a for loop inside the function and I cannot get UUU to give me an 
updated grid.dens object when I run the function (it does update when I 
run just the for loop). Here's a simplified version of my function:

UUU=function(pop, grid.dens) {

for (i in 1:10){
Food=grid.dens[pop$yloc[i],pop$xloc[i]]   #use initial grid.dens values
Consumed=(pop$weight[i]*0.25)
Left=Food-Consumed
grid.dens[pop$yloc[i],pop$xloc[i]]=Left   #update grid.dens values on i
pop$birth[i]=pop$birth[i]+1
}
return(pop)
return(grid.dens)
}

I get an updated pop, but not an updated grid.dens.  What am I doing wrong?

Thanks!

Nico



More information about the R-help mailing list