Re: [R] programming creating different functions in a loop

2009-03-26 Thread Bert Gunter
ect.org] On Behalf Of Greg Snow Sent: Thursday, March 26, 2009 9:54 AM To: Bert Gunter; 'Florin Maican'; r-help@r-project.org Subject: Re: [R] programming creating different functions in a loop But wrong questions requiring complicated answers are sometimes more fun :-). One difference

Re: [R] programming creating different functions in a loop

2009-03-26 Thread Greg Snow
n...@r- > project.org] On > Behalf Of Greg Snow > Sent: Thursday, March 26, 2009 9:25 AM > To: Florin Maican; r-help@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 > consid

Re: [R] programming creating different functions in a loop

2009-03-26 Thread Bert Gunter
: Thursday, March 26, 2009 9:18 AM > To: r-help@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+

Re: [R] programming creating different functions in a loop

2009-03-26 Thread Greg Snow
To: r-help@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 >

Re: [R] programming creating different functions in a loop

2009-03-26 Thread Florin Maican
Thanks Luke! It works! My mistake was that I used "local binding" only for "i" and not for the whole function. Best regards, Florin On Thu, 26 Mar 2009 10:57:21 -0500 (CDT) l...@stat.uiowa.edu wrote: > for() does not creae separete bindings for the index each iteration, > so the function bodi

Re: [R] programming creating different functions in a loop

2009-03-26 Thread luke
for() does not creae separete bindings for the index each iteration, so the function bodies see the global binding of i, which in this case will be the final value. One possible solution is to use local(), e.g. for(i in 1:3){ assign(paste("f",i,sep=""), local({ k <- i

[R] programming creating different functions in a loop

2009-03-26 Thread Florin Maican
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){ ass