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"
--
View this message in context: http://r.789695.n4.nabble.com/filling-array-with-functions-tp3468313p3468313.html
Sent from the R help mailing list archive at Nabble.com.
filling array with functions
7 messages · Jerome Asselin, Richard M. Heiberger, derek +1 more
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.
1 day later
Thats not exactly what I hoped for. But for now it has to suffice. More
transparent syntax would be nicer.
Exactly what I would like to do is:
for (i in 1:9){
f[i]<-function(x){
a*x+b)
}
curve(f[i], 0, 8)
sol[i]<-uniroot(f[i],c(0, 8))$root
points(sol[i],0,pch=16,cex=1,col="red")
}
Perhaps is item for a wish list?
--
View this message in context: http://r.789695.n4.nabble.com/filling-array-with-functions-tp3468313p3471463.html
Sent from the R help mailing list archive at Nabble.com.
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20110424/8a537133/attachment.pl>
Richard, that way I will have to write functions manually and that is not possible for large number of functions. derek -- View this message in context: http://r.789695.n4.nabble.com/filling-array-with-functions-tp3468313p3472885.html Sent from the R help mailing list archive at Nabble.com.
On Apr 25, 2011, at 12:15 , derek wrote:
Richard, that way I will have to write functions manually and that is not possible for large number of functions.
Well do what he means:
fv <- vector("list",10)
for (i...
{
...
fv[[i]] <- ...
...
}
(Your code still won't work as written; V[i] will not be evaluated until you call the function(s), likely to V[10]. Try something like fv[[i]] <- local({zz <- V[i]; function(x) zz - b*a*x}), or play around with eval(substitute(....)) )
derek -- View this message in context: http://r.789695.n4.nabble.com/filling-array-with-functions-tp3468313p3472885.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.
Peter Dalgaard Center for Statistics, Copenhagen Business School Solbjerg Plads 3, 2000 Frederiksberg, Denmark Phone: (+45)38153501 Email: pd.mes at cbs.dk Priv: PDalgd at gmail.com
Still I haven't had any luck yet. How about defining new function and its domain, is it somehow possible? Like this: a<-function(x) x beolongs to natural numbers <0,100> -- View this message in context: http://r.789695.n4.nabble.com/filling-array-with-functions-tp3468313p3473300.html Sent from the R help mailing list archive at Nabble.com.