[R] [SOLVED] Two functions as parametrs of a function.

Duncan Murdoch murdoch.duncan at gmail.com
Thu Apr 7 16:47:03 CEST 2011


On 07/04/2011 10:12 AM, KENNETH R CABRERA wrote:
> Dr. Murdoch:
>
> Thank you very much for your help.
>
> Where can I find a more systematic documentation about these topics?
>
> I mean, examples of "do.call", environments, "as.function", the
> scoping and the way understand when to use "force", etc.

For details of the concepts, you should look in the "R Language 
Definition" manual (and perhaps "An Introduction to R" for some more 
examples).

For individual functions, the help page for each usually includes some 
examples.

And then there's the source code, but if the manuals are unclear, it may 
be no clearer.

Duncan Murdoch


> Again, thank you.
>
> Kenneth
>
> ----- Mensaje original -----
> De: Duncan Murdoch<murdoch.duncan at gmail.com>
> Fecha: Jueves, 7 de Abril de 2011, 7:04 am
> Asunto: Re: [R] Two functions as parametrs of a function.
> A: Kenneth Roy Cabrera Torres<krcabrer at une.net.co>
> CC: r-help<r-help at r-project.org>
>
> >  On 07/04/2011 7:45 AM, Kenneth Roy Cabrera Torres wrote:
> >  >Hi R users:
> >  >
> >  >I'm trying to make a function where two of the parameters are
> >  >functions, but I don't know how to put each set of parameters for
> >  >each function.
> >  >
> >  >What am I missing?
> >  >
> >  >I try this code:
> >  >
> >  >f2<-
> >  function(n=2,nsim=100,fun1=rnorm,par1=list(),fun2=rnorm,par2=list()){>      force(fun1)
> >  >      force(fun2)
> >  >      force(n)
> >  >      p1<-unlist(par1)
> >  >      p2<-unlist(par2)
> >
> >  You don't want to unlist those.  Leave them as lists, and
> >  use the do.call() function for the calls.  For example,
> >
> >  localfun1<- function() do.call(fun1, c(list(n), par1))
> >
> >
> >  >      force(p1)
> >  >      force(p2)
> >
> >  Calling force() is only useful for arguments to your
> >  function:  it kicks the evaluator, so it's not so
> >  lazy.   Since p1 and p2 were calculated earlier,
> >  there's no need to force them.  You might need to force n,
> >  since it is only used in the localfun1 and localfun2 calls, and
> >  if the argument to f2 changes before you make those calls, the
> >  wrong value will be used.  That's pretty unlikely with your
> >  code, but since fun1 and fun2 can do anything, it's safest to
> >  fix the value of n before you call them.  The same argument
> >  applies to fun1 and fun2 themselves.
> >
> >  Duncan Murdoch
> >
> >  >      localfun1<- function() fun1(n, p1)
> >  >      localfun2<- function() fun2(n, p2)
> >  >      vp<-
> >  replicate(nsim,t.test(localfun1(), localfun2())$p.value)
> >  >      return(vp)
> >  >}
> >  >
> >  >f2(fun1=rbeta,par1=list(shape1=2,shape2=2),fun2=rbeta,par2=list(shape1=1,shape2=2))
> >  >
> >  >Thank you for your help.
> >  >
> >  >Kenneth
> >  >
> >  >______________________________________________
> >  >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