[R] Creating functions with a loop.

David Winsemius dwinsemius at comcast.net
Tue May 22 17:08:20 CEST 2012


On May 22, 2012, at 10:06 AM, Etienne Larrivée-Hardy wrote:

> Hi
>
> I am trying to create n functions where each function is defined in  
> function one step before, i.e. something like
>
> ff.k(x) = ff.j(x) - sum(1:j),      for j=k-1

There is a cumsum function:

 > cumsum(1:10)
  [1]  1  3  6 10 15 21 28 36 45 55

Did you mean something like?:

  ff.k[j] <- ff.j[j] - sum(1:j),      for j=k-1

In R the paren, "(", indicates that you are calling a function whereas  
you appear interested in making an indexed assignment to a vector.

>
> Is it possible? If it isn't and I manually create each function then  
> is their a way to call them through a loop?  My objective is to  
> calculate something like
>
> result.k = ff.k(x1)/ff.k(x2)      for k in 2:n

You may have clear idea about which k-indices should go where in that  
expression, but I surely do not. What are 'x1' and 'x2'?

-- 

David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list