[R] RE : Creating functions with a loop.
Etienne Larrivée-Hardy
etienne.larrivee-hardy.1 at ulaval.ca
Tue May 22 17:12:20 CEST 2012
I must say I'm new here and I don't quite know how things work so you will have to excuse me.
The first part of my problem is that it would help me greatly to have a loop creating all the functions. Here is an example of how the 10th function would need to look like:
ff10 <- function (q, alph, lam)
{
ff9(q, alph, lam) - sum(v.pp[1:(10 - 1)] * exp(-v.beta[1:(10 - 1)] * cc1 * 10^(1:(10 - 1) - 1)))
}
where v.pp and v.beta are vector for which values are known up to 9 in this case. cc1, bb, alpha, lambda are fixed.
The second part of my problem is that with those functions I need to calculate the next value of v.beta and v.pp.
v.beta[10] <- 1/((bb-1)*cc1*10^-(10-1))*log(ff2(cc1*10^-(10-1),alpha,lambda)/ff10(cc1*10^-(10-1)*bb,alpha,lambda))
and
v.pp[10] <- ...
I don't know if that made it any clearer...
________________________________________
De : David Winsemius [dwinsemius at comcast.net]
Date d'envoi : 22 mai 2012 11:08
À : Etienne Larrivée-Hardy
Cc : r-help at r-project.org
Objet : Re: [R] Creating functions with a loop.
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