[R] Creating functions with a loop.

R. Michael Weylandt michael.weylandt at gmail.com
Wed May 23 00:50:08 CEST 2012


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.



More information about the R-help mailing list