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.. Put more generally, is there a way of making R "understand" automatically that funlist above is really a function of x? 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? Thanks in advance Søren Højsgaard [[alternative HTML version deleted]]
Hi, Once again, I think apply family is your friend. Dont forget functions are objects as any others: At 09:36 5/04/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.> lapply(funlist, FUN=function(f) f(10)) [[1]] [1] 10 [[2]] [1] 100 Or, better, pass the value as extra-parameter: > sapply(funlist, FUN=function(f,x) f(x), x=10) [1] 10 100>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?Here again, you can embedd the definition of your function in the [sl]apply code: > sapply(x<-10, FUN=function(x) x^2) [1] 100 Eric Eric Lecoutre UCL / Institut de Statistique Voie du Roman Pays, 20 1348 Louvain-la-Neuve Belgium tel: (+32)(0)10473050 lecoutre at stat.ucl.ac.be http://www.stat.ucl.ac.be/ISpersonnel/lecoutre If the statistics are boring, then you've got the wrong numbers. -Edward Tufte
> 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. How about sapply(funlist, function(x) x(10)) ? > 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) Hadley
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, ripley at stats.ox.ac.uk 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
Maybe Matching Threads
- Deparsing part of a list argument
- The fastest way to select and execute a few selected functions inside a function
- Discrepancies in run times
- Calling the curve function with a character object converted into an expression
- how Can make function for selecting the products