On Mon, 5 Apr 2004, S�ren H�jsgaard wrote:

> Suppose I have
>     f1 <- function(x) x
>     f2 <- function(x) x^2
>     funlist <- list(f1,f2)
> Then I would like to evaluate funlist such that when x is 10 I should get a list 
> with 10 and 100.
> 
> A naive way of doint this is 
>     myf <- funlist[[1]]
>     do.call(paste(quote(myf)), list(x=10))
>     myf <- funlist[[2]]
>     do.call(paste(quote(myf)), list(x=10))

> - but there has to be much more elegant ways of doing this. I just can't
> figure out how..

lapply(funlist, function(f, x)f(x), x=10)

> Put more generally, is there a way of making R "understand"
> automatically that funlist above is really a function of x?

It isn't.  It is a list of functions of , so use lapply.

> A related question is that of anonymous functions: how to evaluate
> function(x)x^2 on x<-10 without assigning the function to a name?

> (function(x)x^2)(10)
[1] 100


-- 
Brian D. Ripley,                  [EMAIL PROTECTED]
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

______________________________________________
[EMAIL PROTECTED] mailing list
https://www.stat.math.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html

Reply via email to