Skip to content
Prev 102367 / 398500 Next

summing elements in a list of functions

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

  
    
Message-ID: <200610201432.k9KEWxew016851@definetti.ddns.uark.edu>
In-Reply-To: <200610201519.06442.jf117@york.ac.uk> (message from James Foadi on Fri, 20 Oct 2006 15:19:06 +0100)