Dear all, I have looked for an answer for a couple of days, but can't come with any solution. I have a set of functions, say:> t0 <- function(x) {1} > t1 <- function(x) {x} > t2 <- function(x) {x^2} > t3 <- function(x) {x^3}I would like to find a way to add up the previous 4 functions and obtain a new function:> rrr <- function(x) {1+x+x^2+x^3}without, actually, having to write it in the previous form (I could have cases with hundreds of functions). I thought that perhaps I could first define a list of functions:> ttt <- list(t0,t1,t2,t3)and then I could use something like "sum", to add up all the elements of the list and obtain another function. I've tried:> rrr <- function(x) {sum(ttt)}but it does not work. Any help with this is greatly appreciated. Cheers, james -- Dr James Foadi Department of Physics University of York York YO10 5DD email: jf117 at york.ac.uk web page: http://www-users.york.ac.uk/~jf117 Tel: 0044 1904 434622 Mobile: 0044 7740 678548
Try this: L <- list(function(x) 1, function(x) x, sin, cos) sumL <- function(x) sum(sapply(L, function(f) f(x))) sumL(pi) # pi On 10/20/06, James Foadi <jf117 at york.ac.uk> wrote:> Dear all, > I have looked for an answer for a couple of days, but can't come with any > solution. > > I have a set of functions, say: > > > t0 <- function(x) {1} > > t1 <- function(x) {x} > > t2 <- function(x) {x^2} > > t3 <- function(x) {x^3} > > I would like to find a way to add up the previous 4 functions and obtain a new > function: > > > rrr <- function(x) {1+x+x^2+x^3} > > without, actually, having to write it in the previous form (I could have cases > with hundreds of functions). I thought that perhaps I could first define a > list of functions: > > > ttt <- list(t0,t1,t2,t3) > > and then I could use something like "sum", to add up all the elements of the > list and obtain another function. I've tried: > > > rrr <- function(x) {sum(ttt)} > > but it does not work. > > Any help with this is greatly appreciated. > > Cheers, > > james > -- > Dr James Foadi > Department of Physics > University of York > York YO10 5DD > > email: jf117 at york.ac.uk > web page: http://www-users.york.ac.uk/~jf117 > Tel: 0044 1904 434622 > Mobile: 0044 7740 678548 > > ______________________________________________ > R-help at stat.math.ethz.ch 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. >
James Foadi <jf117 at york.ac.uk> writes:> Dear all, > I have looked for an answer for a couple of days, but can't come with any > solution. > > I have a set of functions, say: > > > t0 <- function(x) {1} > > t1 <- function(x) {x} > > t2 <- function(x) {x^2} > > t3 <- function(x) {x^3} > > I would like to find a way to add up the previous 4 functions and obtain a new > function: > > > rrr <- function(x) {1+x+x^2+x^3} > > without, actually, having to write it in the previous form (I could have cases > with hundreds of functions). I thought that perhaps I could first define a > list of functions: > > > ttt <- list(t0,t1,t2,t3) > > and then I could use something like "sum", to add up all the elements of the > list and obtain another function. I've tried: > > > rrr <- function(x) {sum(ttt)} > > but it does not work. > > Any help with this is greatly appreciated.rrr <- function(x) sum(sapply(ttt,function(f)f(x))) -- O__ ---- Peter Dalgaard ?ster Farimagsgade 5, Entr.B c/ /'_ --- Dept. of Biostatistics PO Box 2099, 1014 Cph. K (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907
will this work for you?> t0 <- function(x) {1} > t1 <- function(x) {x} > t2 <- function(x) {x^2} > t3 <- function(x) {x^3} > > t.l <- list(t0,t1,t2,t3) > t.l[[1]] function(x) {1} [[2]] function(x) {x} [[3]] function(x) {x^2} [[4]] function(x) {x^3}> arg.val <- 4 # evaluate for 4> sum(unlist(lapply(t.l, function(x)x(arg.val))))[1] 85 On 10/20/06, James Foadi <jf117 at york.ac.uk> wrote:> Dear all, > I have looked for an answer for a couple of days, but can't come with any > solution. > > I have a set of functions, say: > > > t0 <- function(x) {1} > > t1 <- function(x) {x} > > t2 <- function(x) {x^2} > > t3 <- function(x) {x^3} > > I would like to find a way to add up the previous 4 functions and obtain a new > function: > > > rrr <- function(x) {1+x+x^2+x^3} > > without, actually, having to write it in the previous form (I could have cases > with hundreds of functions). I thought that perhaps I could first define a > list of functions: > > > ttt <- list(t0,t1,t2,t3) > > and then I could use something like "sum", to add up all the elements of the > list and obtain another function. I've tried: > > > rrr <- function(x) {sum(ttt)} > > but it does not work. > > Any help with this is greatly appreciated. > > Cheers, > > james > -- > Dr James Foadi > Department of Physics > University of York > York YO10 5DD > > email: jf117 at york.ac.uk > web page: http://www-users.york.ac.uk/~jf117 > Tel: 0044 1904 434622 > Mobile: 0044 7740 678548 > > ______________________________________________ > R-help at stat.math.ethz.ch 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. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem you are trying to solve?
Here is one way. To have a vectorized version you need to redefine 't0', though t0 <- function(x) {1} t1 <- function(x) {x} t2 <- function(x) {x^2} t3 <- function(x) {x^3} ttt <- list(t0,t1,t2,t3) rrr <- function(x) sum(sapply(seq(along=ttt), function(i) ttt[[i]](x))) ## vectorized version ttt[[1]] <- t0 <- function(x) rep(1, length(x)) rrr2 <- function(x) rowSums(sapply(seq(along=ttt), function(i) ttt[[i]](x))) Hope this helps, Giovanni> Date: Fri, 20 Oct 2006 15:19:06 +0100 > From: James Foadi <jf117 at york.ac.uk> > Sender: r-help-bounces at stat.math.ethz.ch > Precedence: list > User-Agent: KMail/1.9.4 > > Dear all, > I have looked for an answer for a couple of days, but can't come with any > solution. > > I have a set of functions, say: > > > t0 <- function(x) {1} > > t1 <- function(x) {x} > > t2 <- function(x) {x^2} > > t3 <- function(x) {x^3} > > I would like to find a way to add up the previous 4 functions and obtain a new > function: > > > rrr <- function(x) {1+x+x^2+x^3} > > without, actually, having to write it in the previous form (I could have cases > with hundreds of functions). I thought that perhaps I could first define a > list of functions: > > > ttt <- list(t0,t1,t2,t3) > > and then I could use something like "sum", to add up all the elements of the > list and obtain another function. I've tried: > > > rrr <- function(x) {sum(ttt)} > > but it does not work. > > Any help with this is greatly appreciated. > > Cheers, > > james > -- > Dr James Foadi > Department of Physics > University of York > York YO10 5DD > > email: jf117 at york.ac.uk > web page: http://www-users.york.ac.uk/~jf117 > Tel: 0044 1904 434622 > Mobile: 0044 7740 678548 > > ______________________________________________ > R-help at stat.math.ethz.ch 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. > >-- __________________________________________________ [ ] [ Giovanni Petris GPetris at uark.edu ] [ Department of Mathematical Sciences ] [ University of Arkansas - Fayetteville, AR 72701 ] [ Ph: (479) 575-6324, 575-8630 (fax) ] [ http://definetti.uark.edu/~gpetris/ ] [__________________________________________________]
Many thanks to those who have answered my question. Could I ask Gabor and Peter the meaning of:> sum(sapply(ttt,function(f) f(x)))I gather that a "mysterious" function f(x) is applied to all components of ttt, and sum can act on this modified object. But what is exactly f? And how does the list object change? Cheers, J -- Dr James Foadi Department of Physics University of York York YO10 5DD email: jf117 at york.ac.uk web page: http://www-users.york.ac.uk/~jf117 Tel: 0044 1904 434622 Mobile: 0044 7740 678548
ttt is a list of functions so each function in ttt is passed in turn to the anonymous function as argument f. On 10/20/06, James Foadi <jf117 at york.ac.uk> wrote:> Many thanks to those who have answered my question. > Could I ask Gabor and Peter the meaning of: > > > sum(sapply(ttt,function(f) f(x))) > > I gather that a "mysterious" function f(x) is applied to all components of > ttt, and sum can act on this modified object. But what is exactly f? And how > does the list object change? > > Cheers, > > J > > -- > Dr James Foadi > Department of Physics > University of York > York YO10 5DD > > email: jf117 at york.ac.uk > web page: http://www-users.york.ac.uk/~jf117 > Tel: 0044 1904 434622 > Mobile: 0044 7740 678548 >