[R] filling array with functions

Jerome Asselin jerome.asselin.stat at gmail.com
Fri Apr 22 19:18:06 CEST 2011


On Fri, 2011-04-22 at 10:02 -0700, derek wrote:
> Hello R,
> 
> I would like to find out how to generate array full of functions. I tried it
> like this:
> 
> fv=array(,dim=c(1,10))
> V=c(1:10)
> for (i in 1:10){
> fv[i]<-function(x)(V[i]-b*a*x) # b, x are constants.
> }
> 
> But it returns:
> "incompatible types (from closure to logical) in subassignment type fix"
> 

You could parse individual expressions.

fv=array(,dim=c(1,10))
V=c(1:10)
for (i in 1:10){
fv[i]<-parse(text=paste("function(x)(V[", i, "]-b*a*x)", sep=""))
}

a=1;b=1
eval(fv[3])(7)

Hope this helps.



More information about the R-help mailing list