On Wed, Apr 27, 2011 at 12:58 PM, Nick Sabbe <nick.sa...@ugent.be> wrote: > No, that does not work. > You cannot do assignment within (l)apply. > Nor in any other function for that matter.
Yes that may work if you want to. You can do non-local assignment within lapply using <<- (and, for that matter, within any other function) but there is no one-word answer to the question whether this is a good idea. a <- list() lapply(1:5, function(x) a[[x]] <<- x) print(a) KK > > > Nick Sabbe > -- > ping: nick.sa...@ugent.be > link: http://biomath.ugent.be > wink: A1.056, Coupure Links 653, 9000 Gent > ring: 09/264.59.36 > > -- Do Not Disapprove > > > > -----Original Message----- > From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On > Behalf Of Alaios > Sent: woensdag 27 april 2011 11:37 > To: R-help@r-project.org > Subject: [R] Assignments inside lapply > > 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 > > ______________________________________________ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. > > ______________________________________________ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. > ______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.