On Aug 31, 2011, at 11:05 AM, Bert Gunter wrote:


In R functions are "things" but they are not "strings" and cannot be
executed by just asserting a character vector element.

... But note that they can be executed by by asserting a list of
functions component (as well as by do.call below)

x <- c(rnorm, rt, rweibull) ## this is a list of functions
x[[2]](10,df=5)
[1] -1.03683857 1.20245119 -2.77762457 -0.02124206 -0.78748356 1.36294023
[7]  0.50626709 -0.37386404  0.34371389 -1.28934128

And the OP may want to also look at the examples in:

?match.arg
?match.fun

And ponder these results:

> funlist <- list(dgamma, dnorm, dexp)
> "dgamma" %in% funlist
[1] FALSE

> class(funlist)
[1] "list"
> class(funlist[1])
[1] "list"
> class(funlist[[1]])
[1] "function"


> match.fun("dgamma")
function (x, shape, rate = 1, scale = 1/rate, log = FALSE)
.Internal(dgamma(x, shape, scale, log))
<environment: namespace:stats>



--
David.


Cheers,
Bert


If you want to
construct a 'call' from a a character, you can bridge that divide with
do.call:

?do.call
fn <- paste("d", "gamma", sep="")
do.call(fn, list(1:10, shape=1) )
[1] 0.36787944117 0.13533528324 0.04978706837 0.01831563889 0.00673794700 [6] 0.00247875218 0.00091188197 0.00033546263 0.00012340980 0.00004539993

--
David


David Winsemius, MD
West Hartford, CT

______________________________________________
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
and provide commented, minimal, self-contained, reproducible code.

Reply via email to