Hello there I have a list, Y, and each component of that list is a real-valued function (that is, Y[[i]](u) returns a number). I was wishing to build the mean function and the first thing I thought of was Ybar<-function(u){ mean(Y[[1:n]](u)) } but obviously this doesn't work, since Y[[1:n]] is not allowed at all. Any elegant suggestions? I really would like to avoid something like Ybar<-function(u){ for (i in 1:n){ Ybar<-Ybar + Ybar[[i]](u) } Ybar<-1/n*Ybar } Thanks in advance! Best regards, Eduardo Horta [[alternative HTML version deleted]]
Try this: u <- 1:10 mean(sapply(Y, function(f)match.fun(f)(u))) On Tue, Nov 16, 2010 at 9:00 PM, Eduardo de Oliveira Horta < eduardo.oliveirahorta@gmail.com> wrote:> Hello there > > I have a list, Y, and each component of that list is a real-valued function > (that is, Y[[i]](u) returns a number). > > I was wishing to build the mean function and the first thing I thought of > was > > Ybar<-function(u){ > mean(Y[[1:n]](u)) > } > > but obviously this doesn't work, since Y[[1:n]] is not allowed at all. > > Any elegant suggestions? I really would like to avoid something like > > Ybar<-function(u){ > for (i in 1:n){ > Ybar<-Ybar + Ybar[[i]](u) > } > Ybar<-1/n*Ybar > } > > Thanks in advance! > > Best regards, > > Eduardo Horta > > [[alternative HTML version deleted]] > > ______________________________________________ > 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. >-- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40" S 49° 16' 22" O [[alternative HTML version deleted]]
Eduardo - I'd guess that Ybar = function(u)mean(sapply(Y,function(fun)fun(u))) will do what you want, but without a reproducible example, it's hard to tell. - Phil Spector Statistical Computing Facility Department of Statistics UC Berkeley spector at stat.berkeley.edu On Tue, 16 Nov 2010, Eduardo de Oliveira Horta wrote:> Hello there > > I have a list, Y, and each component of that list is a real-valued function > (that is, Y[[i]](u) returns a number). > > I was wishing to build the mean function and the first thing I thought of > was > > Ybar<-function(u){ > mean(Y[[1:n]](u)) > } > > but obviously this doesn't work, since Y[[1:n]] is not allowed at all. > > Any elegant suggestions? I really would like to avoid something like > > Ybar<-function(u){ > for (i in 1:n){ > Ybar<-Ybar + Ybar[[i]](u) > } > Ybar<-1/n*Ybar > } > > Thanks in advance! > > Best regards, > > Eduardo Horta > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at 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. >