[R] Assignments inside lapply

Alaios alaios at yahoo.com
Wed Apr 27 11:36:55 CEST 2011


Dear all I would like to ask you if an assignment can be done inside a lapply statement.

For example

I would like to covert a double nested for loop

for (i in c(1:dimx)){ 
  for (j in c(1:dimy)){
      Powermap[i,j] <- Pr(c(i,j),c(PRX,PRY),f)
   }
}

to something like that:


ij<-expand.grid(i=seq(1:dimx),j=(1:dimy))

unlist(lapply(1:nrow(ij),function(rowId) { return (Powermap[i,j]<-Pr(c(ij$i[rowId],ij$j[rowId]),c(PRX,PRY),f))   }))


as you can see lapply does not return nothing as the assignment is done inside the function. Would that work correctly? What are the cases such a statement will misfunction?

I would like to thank you in advace for your help.

Best Regards
Alex



More information about the R-help mailing list