# [R] programming creating different functions in a loop

Greg Snow Greg.Snow at imail.org
Thu Mar 26 17:53:35 CET 2009

```But wrong questions requiring complicated answers are sometimes more fun :-).

One difference though is in my last example, your function will give a different answer for f(1:3, 1:10), but for the simpler cases, yours is probably the better.

--
Gregory (Greg) L. Snow Ph.D.
Statistical Data Center
Intermountain Healthcare
greg.snow at imail.org
801.408.8111

> -----Original Message-----
> From: Bert Gunter [mailto:gunter.berton at gene.com]
> Sent: Thursday, March 26, 2009 10:33 AM
> To: Greg Snow; 'Florin Maican'; r-help at r-project.org
> Subject: RE: [R] programming creating different functions in a loop
>
> ... but perhaps even more straightforward is:
>
> f <- function(i,x)x+i
>
> Of course, we don't know exactly what the poster is after with his
> rather
> arcane construction, so this may not suit. But you know the rule: if
> the
> question requires a complex, tricky answer, it's probably the wrong
> question.
>
> Cheers,
> Bert
>
> Bert Gunter
> Genentech Nonclinical Biostatistics
>
>
> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
> project.org] On
> Behalf Of Greg Snow
> Sent: Thursday, March 26, 2009 9:25 AM
> To: Florin Maican; r-help at r-project.org
> Subject: Re: [R] programming creating different functions in a loop
>
> Anytime that you are tempted to use assign and a loop, you should
> consider
> using lapply (or sapply) and a list instead.
>
> Consider this alternative:
>
> > f <- lapply( 1:3, function(i){ force(i); function(x) x+i} )
> >
> > f[[1]](3)
> [1] 4
> > f[[2]](10)
> [1] 12
> > f[[3]](0)
> [1] 3
> >
> > sapply( f, function(f) f(1:10) )
>       [,1] [,2] [,3]
>  [1,]    2    3    4
>  [2,]    3    4    5
>  [3,]    4    5    6
>  [4,]    5    6    7
>  [5,]    6    7    8
>  [6,]    7    8    9
>  [7,]    8    9   10
>  [8,]    9   10   11
>  [9,]   10   11   12
> [10,]   11   12   13
>
> Hope this helps,
>
> --
> Gregory (Greg) L. Snow Ph.D.
> Statistical Data Center
> Intermountain Healthcare
> greg.snow at imail.org
> 801.408.8111
>
>
> > -----Original Message-----
> > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
> > project.org] On Behalf Of Florin Maican
> > Sent: Thursday, March 26, 2009 9:18 AM
> > To: r-help at r-project.org
> > Subject: [R] programming creating different functions in a loop
> >
> > Hi
> >
> > I want to create the  following functions in a loop
> >
> > f1<-function(x){x+1}
> > f2<-function(x){x+2}
> > f3<-function(x){x+3}
> >
> > Output   f1(2)=3
> >          f2(2)=4
> >          f3(2)=5
> >
> >
> > I tried to create the in a loop as bellow but I get wrong on answers
> > because the value of i change
> >
> > for(i  in 1:3){
> >     assign(paste("f",i,sep="")
> >             ,function(x)
> >              x+i
> >             )
> > } # end for
> >
> > Output   f1(2)=5
> >          f2(2)=5
> >          f3(2)=5
> > But it is not what I want. The question is how I can
> > fix in R the value of "i" in my functions?  I tried to use assign()
> and
> > get(),but I did not manage.
> >
> > Florin
> >
> > --
> >          Florin  Maican
> > ==================================
> >
> > Department of Economics,
> > School of Business, Economics and Law,
> > Gothenburg University, Sweden
> > -----------------------------------
> >     P.O. Box 640 SE-405 30,
> >     Gothenburg, Sweden
> >
> >  Mobil:  +46 76 235 3039
> >  Phone:  +46 31 786 4866
> >  Fax:    +46 31 786 4154
> >  E-mail: florin.maican at handels.gu.se
> > ------------------------------------
> >  "Not everything that counts can be
> >  counted, and not everything that can be
> >  counted counts."
> >                          --- Einstein ---
> >
> > ______________________________________________
> > R-help at r-project.org mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-help
> > 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