I am having trouble figuring out how to use do.call to call and run a list of functions. for example: make.draw = function(i){i;function()runif(i)} function.list = list() for (i in 1:3) function.list[[i]] = make.draw(i) will result in> function.list[[1]]()[1] 0.2996515> function.list[[2]]()[1] 0.7276203 0.4704813> function.list[[3]]()[1] 0.9092999 0.7307774 0.4647443 what I want to do is create a function that calls all three functions in the list at one go. from what I understand as.call() can be used to do this but I am having trouble connecting the dots and getting 6 uniform random draws from function.list. thanks, honeyoak. -- View this message in context: http://r.789695.n4.nabble.com/using-do-call-to-call-a-list-of-functions-tp3906337p3906337.html Sent from the R help mailing list archive at Nabble.com.
> function.list=c(sin, cos, function(x) tan(x)) > for (f in function.list) print(f(pi))[1] 1.224606e-16 [1] -1 [1] -1.224606e-16>On Fri, Oct 14, 2011 at 5:48 PM, honeyoak <honeyoak@gmail.com> wrote:> I am having trouble figuring out how to use do.call to call and run a list > of > functions. > > for example: > > make.draw = function(i){i;function()runif(i)} > function.list = list() > for (i in 1:3) function.list[[i]] = make.draw(i) > > will result in > > > function.list[[1]]() > [1] 0.2996515 > > function.list[[2]]() > [1] 0.7276203 0.4704813 > > function.list[[3]]() > [1] 0.9092999 0.7307774 0.4647443 > > what I want to do is create a function that calls all three functions in > the > list at one go. from what I understand as.call() can be used to do this but > I am having trouble connecting the dots and getting 6 uniform random draws > from function.list. thanks, honeyoak. > > -- > View this message in context: > http://r.789695.n4.nabble.com/using-do-call-to-call-a-list-of-functions-tp3906337p3906337.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help@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<http://www.r-project.org/posting-guide.html> > and provide commented, minimal, self-contained, reproducible code. >[[alternative HTML version deleted]]
package plyr makes it easier, plyr::each(function.list)(pi) HTH, baptiste On 15 October 2011 11:55, Richard M. Heiberger <rmh at temple.edu> wrote:>> function.list=c(sin, cos, function(x) tan(x)) >> for (f in function.list) print(f(pi)) > [1] 1.224606e-16 > [1] -1 > [1] -1.224606e-16 >> > > On Fri, Oct 14, 2011 at 5:48 PM, honeyoak <honeyoak at gmail.com> wrote: > >> I am having trouble figuring out how to use do.call to call and run a list >> of >> functions. >> >> for example: >> >> make.draw = function(i){i;function()runif(i)} >> function.list = list() >> for (i in 1:3) function.list[[i]] = make.draw(i) >> >> will result in >> >> > function.list[[1]]() >> ?[1] 0.2996515 >> > function.list[[2]]() >> ?[1] 0.7276203 0.4704813 >> > function.list[[3]]() >> ?[1] 0.9092999 0.7307774 0.4647443 >> >> what I want to do is create a function that calls all three functions in >> the >> list at one go. from what I understand as.call() can be used to do this but >> I am having trouble connecting the dots and getting 6 uniform random draws >> from function.list. thanks, honeyoak. >> >> -- >> View this message in context: >> http://r.789695.n4.nabble.com/using-do-call-to-call-a-list-of-functions-tp3906337p3906337.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<http://www.r-project.org/posting-guide.html> >> and provide commented, minimal, self-contained, reproducible code. >> > > ? ? ? ?[[alternative HTML version deleted]] > > ______________________________________________ > 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. >
Thanks for the reference, the each function in the plyr package is exactly what I wanted. -- View this message in context: http://r.789695.n4.nabble.com/using-do-call-to-call-a-list-of-functions-tp3906337p3906574.html Sent from the R help mailing list archive at Nabble.com.
On Oct 14, 2011, at 8:14 PM, honeyoak wrote:> Thanks for the reference, the each function in the plyr package is > exactly > what I wanted.I doubt it. You have not looked at the `each` code. Its just a wrapper for a for loop and on StackOverfolw you started out saying that you were rejecting for() loops and sapply() solutions because you thought they were too slow. And you did not want to put any work into doing performance analysis of your 40 or so functions that you planned on testing .... so, as they say these days "good luck with that".> > -- > View this message in context: http://r.789695.n4.nabble.com/using-do-call-to-call-a-list-of-functions-tp3906337p3906574.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.David Winsemius, MD West Hartford, CT