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
unlist(lapply(1:nrow(ij),function(rowId) { return (Powermap[i,j]<-Pr(c(ij$i[rowId],ij$j[rowId]),c(PRX,PRY),f)) }))lapply actually catches each return value of the excuted function. here your function actually returns nothing if the assignment succeeds. If your purpose for the call to Pr is just its result, then you don't need place an assignment here. The lapply can automatically catch the result series. At 2011-04-27 17:36:55£¬Alaios <alaios@yahoo.com> wrote:>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.[[alternative HTML version deleted]]
No, that does not work. You cannot do assignment within (l)apply. Nor in any other function for that matter. Nick Sabbe -- ping: nick.sabbe at 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-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Alaios Sent: woensdag 27 april 2011 11:37 To: R-help at 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 at 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.
Dear Alex, I think you want to use apply() ij <- expand.grid(i = seq_len(dimx),j = seq_len(dimy)) Powermap <- apply(ij, 1, function(x){ Pr(x, c(PRX, PRY), f) }) Best regards, Thierry ---------------------------------------------------------------------------- ir. Thierry Onkelinx Instituut voor natuur- en bosonderzoek team Biometrie & Kwaliteitszorg Gaverstraat 4 9500 Geraardsbergen Belgium Research Institute for Nature and Forest team Biometrics & Quality Assurance Gaverstraat 4 9500 Geraardsbergen Belgium tel. + 32 54/436 185 Thierry.Onkelinx at inbo.be www.inbo.be To call in the statistician after the experiment is done may be no more than asking him to perform a post-mortem examination: he may be able to say what the experiment died of. ~ Sir Ronald Aylmer Fisher The plural of anecdote is not data. ~ Roger Brinner The combination of some data and an aching desire for an answer does not ensure that a reasonable answer can be extracted from a given body of data. ~ John Tukey> -----Oorspronkelijk bericht----- > Van: r-help-bounces at r-project.org > [mailto:r-help-bounces at r-project.org] Namens Alaios > Verzonden: woensdag 27 april 2011 11:37 > Aan: R-help at r-project.org > Onderwerp: [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 at 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. >
On Wed, Apr 27, 2011 at 12:58 PM, Nick Sabbe <nick.sabbe at 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.sabbe at 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-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On > Behalf Of Alaios > Sent: woensdag 27 april 2011 11:37 > To: R-help at 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 at 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 at 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. >
I would like to use lapply as there is a parallel version of lapply called mclapply. My purpose is to convert 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 that can run in parallel with mclapply: I am not sure then how to store the results of lapply(mclapply) correctly to Powermap matrix. That is the reason I put the assignment inside the return statement. For example this does not do what I want to Powermap[i,j]<-unlist(mclapply(1:nrow(ij),function(rowId) { return (Pr(c(ij$i[rowId],ij$j[rowId]),c(PRX,PRY),f) How can I try this out? Best Regards Alex --- On Wed, 4/27/11, ONKELINX, Thierry <Thierry.ONKELINX at inbo.be> wrote:> From: ONKELINX, Thierry <Thierry.ONKELINX at inbo.be> > Subject: RE: [R] Assignments inside lapply > To: "Alaios" <alaios at yahoo.com>, "R-help at r-project.org" <R-help at r-project.org> > Date: Wednesday, April 27, 2011, 11:06 AM > Dear Alex, > > I think you want to use apply() > > ij <- expand.grid(i = seq_len(dimx),j = seq_len(dimy)) > Powermap <- apply(ij, 1, function(x){ > ??? Pr(x, c(PRX, PRY), f) > }) > > Best regards, > > Thierry > > ---------------------------------------------------------------------------- > ir. Thierry Onkelinx > Instituut voor natuur- en bosonderzoek > team Biometrie & Kwaliteitszorg > Gaverstraat 4 > 9500 Geraardsbergen > Belgium > > Research Institute for Nature and Forest > team Biometrics & Quality Assurance > Gaverstraat 4 > 9500 Geraardsbergen > Belgium > > tel. + 32 54/436 185 > Thierry.Onkelinx at inbo.be > www.inbo.be > > To call in the statistician after the experiment is done > may be no more than asking him to perform a post-mortem > examination: he may be able to say what the experiment died > of. > ~ Sir Ronald Aylmer Fisher > > The plural of anecdote is not data. > ~ Roger Brinner > > The combination of some data and an aching desire for an > answer does not ensure that a reasonable answer can be > extracted from a given body of data. > ~ John Tukey > ? > > > -----Oorspronkelijk bericht----- > > Van: r-help-bounces at r-project.org > > > [mailto:r-help-bounces at r-project.org] > Namens Alaios > > Verzonden: woensdag 27 april 2011 11:37 > > Aan: R-help at r-project.org > > Onderwerp: [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 at 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. > >
Here is a solution with lapply PowerMatrix <- matrix(unlist(lapply(seq_len(dimx*dimy), function(x){ i <- 1 + (x - 1) %% dimx j <- 1 + (x - 1) %/% dimy Pr(c(i,j),c(PRX,PRY),f) })), nrow = dimx) A reproducible example dimx <- 5 dimy <- 6 PowerMatrix <- matrix(unlist(lapply(seq_len(dimx*dimy), function(x){ i <- 1 + (x - 1) %% dimx j <- 1 + (x - 1) %/% dimy i * 10 + j })), nrow = dimx) Best regards, Thierry ---------------------------------------------------------------------------- ir. Thierry Onkelinx Instituut voor natuur- en bosonderzoek team Biometrie & Kwaliteitszorg Gaverstraat 4 9500 Geraardsbergen Belgium Research Institute for Nature and Forest team Biometrics & Quality Assurance Gaverstraat 4 9500 Geraardsbergen Belgium tel. + 32 54/436 185 Thierry.Onkelinx at inbo.be www.inbo.be To call in the statistician after the experiment is done may be no more than asking him to perform a post-mortem examination: he may be able to say what the experiment died of. ~ Sir Ronald Aylmer Fisher The plural of anecdote is not data. ~ Roger Brinner The combination of some data and an aching desire for an answer does not ensure that a reasonable answer can be extracted from a given body of data. ~ John Tukey> -----Oorspronkelijk bericht----- > Van: Alaios [mailto:alaios at yahoo.com] > Verzonden: woensdag 27 april 2011 12:14 > Aan: R-help at r-project.org; ONKELINX, Thierry > Onderwerp: RE: [R] Assignments inside lapply > > I would like to use lapply as there is a parallel version of > lapply called mclapply. > > My purpose is to convert > 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 that can run in parallel with mclapply: > I am not sure then how to store the results of > lapply(mclapply) correctly to Powermap matrix. That is the > reason I put the assignment inside the return statement. > For example this does not do what I want to > Powermap[i,j]<-unlist(mclapply(1:nrow(ij),function(rowId) { > return (Pr(c(ij$i[rowId],ij$j[rowId]),c(PRX,PRY),f) > How can I try this out? > > Best Regards > Alex > > --- On Wed, 4/27/11, ONKELINX, Thierry > <Thierry.ONKELINX at inbo.be> wrote: > > > From: ONKELINX, Thierry <Thierry.ONKELINX at inbo.be> > > Subject: RE: [R] Assignments inside lapply > > To: "Alaios" <alaios at yahoo.com>, "R-help at r-project.org" > > <R-help at r-project.org> > > Date: Wednesday, April 27, 2011, 11:06 AM Dear Alex, > > > > I think you want to use apply() > > > > ij <- expand.grid(i = seq_len(dimx),j = seq_len(dimy)) Powermap <- > > apply(ij, 1, function(x){ > > ??? Pr(x, c(PRX, PRY), f) > > }) > > > > Best regards, > > > > Thierry > > > > > ---------------------------------------------------------------------- > > ------ > > ir. Thierry Onkelinx > > Instituut voor natuur- en bosonderzoek team Biometrie & > Kwaliteitszorg > > Gaverstraat 4 9500 Geraardsbergen Belgium > > > > Research Institute for Nature and Forest team Biometrics & Quality > > Assurance Gaverstraat 4 9500 Geraardsbergen Belgium > > > > tel. + 32 54/436 185 > > Thierry.Onkelinx at inbo.be > > www.inbo.be > > > > To call in the statistician after the experiment is done may be no > > more than asking him to perform a post-mortem > > examination: he may be able to say what the experiment died of. > > ~ Sir Ronald Aylmer Fisher > > > > The plural of anecdote is not data. > > ~ Roger Brinner > > > > The combination of some data and an aching desire for an > answer does > > not ensure that a reasonable answer can be extracted from a > given body > > of data. > > ~ John Tukey > > ? > > > > > -----Oorspronkelijk bericht----- > > > Van: r-help-bounces at r-project.org > > > > > [mailto:r-help-bounces at r-project.org] > > Namens Alaios > > > Verzonden: woensdag 27 april 2011 11:37 > > > Aan: R-help at r-project.org > > > Onderwerp: [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 at 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. > > > >