[R] list of funtions
Jonathan Phillips
994phij at gmail.com
Fri Sep 14 01:31:10 CEST 2012
Yes, seen it, and it's obviously the wrong thing to do or I'd be
getting the result I'm looking for. But I can't see the correct way
of doing it. I.e. I can't see any way of setting each element of the
list to a function with a different 'form' value without using some
'i' like variable in a loop.
I don't think it's something obvious I've missed...
On 13 September 2012 18:06, Uwe Ligges <ligges at statistik.tu-dortmund.de> wrote:
>
>
> On 13.09.2012 19:01, Jonathan Phillips wrote:
>>
>> Hi,
>> I have a function called fitMicroProtein which takes a value called
>> form, this can be any integer from 0-38.
>> In a larger function I'm making (it's called Newton), the first thing
>> I want to do is construct a list of functions where form is already
>> set. So in pseudocode
>>
>> fs[[1]](...) <- fitMicroProtein(form=0,...)
>> fs[[2]](...) <- fitMicroProtein(form=1,...)
>> .
>> .
>> .
>>
>> I've tried that and it doesn't work. Here's my code:
>>
>> Newton <- function(metaf,par,niter,dealwith_NA,...)
>> {
>> fs <- list()
>> for(i in 0:(length(par)-1))
>> {
>> fs[[i+1]] <- function(par) return(metaf(par,form=i,...))
>> }
>> .
>> .
>> .
>>
>>
>> and the problem is with the variable 'i'.
>> If I use the debugger, I find that it is specifically that:
>>
>> When it makes f[[1]] we have
>> f[[1]] == function(par) return(metaf(par,form=0,...)
>> but the next thing it does is increment 'i', so f[[1]] becomes
>> function(par) return(metaf(par,form=1,...)
>> where I want f[[1]] to stay as
>> function(par) return(metaf(par,form=0,...)
>>
>> Does anybody know how to stop the value of f[[1]] being dependant on
>> the current value of 'i'?
>
>
> Er, you know that you have
>
> function(par) return(metaf(par,form=i,...))
>
> in your loop. If you want to have it independent if i, why do you specify
> it?
>
> Uwe Ligges
>
>>
>> ______________________________________________
>> 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.
>>
>
More information about the R-help
mailing list