Skip to content
Prev 175022 / 398506 Next

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. 

Thanks in advance,
Florin