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 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 Thank you for your time, Etienne
Creating functions with a loop.
6 messages · David Winsemius, Etienne Larrivée-Hardy, R. Michael Weylandt
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
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
Interpreting your question slightly differently, where ff.k(x) is a
function call.
Suppose you want to get a list of functions like
x^2
x^2 -1
x^2 - 3
x^2 - 6
It's perfectly possible with something like this:
NextFunc <- function(f, i) {
# Takes in a function f and returns
# a different function that gives you
# f(x) - i
force(i) # probably unnecessary, but good practice as this just
might bite you in a loop
function(x) f(x) - i
}
Then
f <- function(x) x^2
f2 <- NextFunc(f, 1)
f3 <- NextFunc(f2, 2)
f3(3) # 6 = 3^2 - 1 - 2
Which should be wrappable in a loop.
Michael
On Tue, May 22, 2012 at 11:08 AM, David Winsemius
<dwinsemius at comcast.net> wrote:
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
______________________________________________ R-help at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Michael's method seems to be working but I still can't get to wrap it in a loop since I cannot get the loop to dynamically change the functions' name, i.e. ff1, ff2, ff3, ... In other words, I would need the first iteration to create the function ff1, the second to create the function ff2, ... Furthermore, does anyone know of a way to call said functions from a loop in a way that the first step of the loop calls ff1, the second calls ff2 and so on? The 2 problems should be closely related I think. Thanks for the time -- View this message in context: http://r.789695.n4.nabble.com/Creating-functions-with-a-loop-tp4630896p4630938.html Sent from the R help mailing list archive at Nabble.com.
You can do what you want with the get() and assign() functions, though it might be easier and better to use match.fun() than get() Much better though would be to build your functions in a list and call the n-th element of the list with syntax like f.list[[3]](4) will call the function in the third element of the list with argument 4. Best, Michael On Tue, May 22, 2012 at 1:24 PM, Etienne Larriv?e-Hardy
<etienne.larrivee-hardy.1 at ulaval.ca> wrote:
Michael's method seems to be working but I still can't get to wrap it in a loop since I cannot get the loop to dynamically change the functions' name, i.e. ff1, ff2, ff3, ... In other words, I would need the first iteration to create the function ff1, the second to create the function ff2, ... Furthermore, does anyone know of a way to call said functions from a loop in a way that the first step of the loop calls ff1, the second calls ff2 and so on? The 2 problems should be closely related I think. Thanks for the time -- View this message in context: http://r.789695.n4.nabble.com/Creating-functions-with-a-loop-tp4630896p4630938.html Sent from the R help mailing list archive at Nabble.com.
______________________________________________ R-help at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.