Re: [R] Assignments inside lapply

From: lcn <lcn918_at_gmail.com>
Date: Wed, 27 Apr 2011 17:58:33 +0800 (CST)

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_at_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_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.

        [[alternative HTML version deleted]]



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. Received on Wed 27 Apr 2011 - 10:04:40 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Wed 27 Apr 2011 - 10:20:34 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive