Dear All, I have a problem which *should* be pretty straightforward to resolve - but I can't work out how! I have a list of 3 coefficient estimates for 4 different datasets: Coefs<-list(c(1,0.6,0.5),c(0.98,0.65,0.4),c(1.05,0.55,0.45),c(0.99,0.50,0.47)) All I want to do is take the sum (or mean) of each coefficient across the 4 datasets. I can do this using a "for" loop, but it gets very messy - as I need to do this several times I was hoping someone might have a better solution using one of the "apply" functions. Any ideas? Many thanks for any help you can provide. Cheers, Nick
Masca, N. wrote:> > Dear All, > > I have a problem which *should* be pretty straightforward to resolve - but > I can't work out how! > > I have a list of 3 coefficient estimates for 4 different datasets: > > Coefs<-list(c(1,0.6,0.5),c(0.98,0.65,0.4),c(1.05,0.55,0.45),c(0.99,0.50,0.47)) > > All I want to do is take the sum (or mean) of each coefficient across the > 4 datasets. > > I can do this using a "for" loop, but it gets very messy - as I need to do > this several times I was hoping someone might have a better solution using > one of the "apply" functions. Any ideas? > > Many thanks for any help you can provide. > > Cheers, > > Nick > >Since Coefs is a list object you could use the lapply "list apply" function to execute the mean() function on each component of the list: lapply( Coefs, mean ) You could also use it to execute your own function that contains additional operations: lapply( Coefs, function( listComponent ){ # Do whatever. mean( listComponent ) }) However, I don't know what you mean by these "coefficients" you want to apply to each of the four data sets. Perhaps you could elaborate? -Charlie ----- Charlie Sharpsteen Undergraduate Environmental Resources Engineering Humboldt State University -- View this message in context: http://www.nabble.com/Which-%22apply%22-function-to-use--tp25439192p25439276.html Sent from the R help mailing list archive at Nabble.com.
Hi, try this, rowMeans(as.data.frame(Coefs)) # or apply(as.data.frame(Coefs), 1, mean) HTH, baptiste 2009/9/14 Masca, N. <nm180@leicester.ac.uk>> Dear All, > > I have a problem which *should* be pretty straightforward to resolve - but > I can't work out how! > > I have a list of 3 coefficient estimates for 4 different datasets: > > > Coefs<-list(c(1,0.6,0.5),c(0.98,0.65,0.4),c(1.05,0.55,0.45),c(0.99,0.50,0.47)) > > All I want to do is take the sum (or mean) of each coefficient across the 4 > datasets. > > I can do this using a "for" loop, but it gets very messy - as I need to do > this several times I was hoping someone might have a better solution using > one of the "apply" functions. Any ideas? > > Many thanks for any help you can provide. > > Cheers, > > Nick > > ______________________________________________ > 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. >-- _____________________________ Baptiste AuguiƩ School of Physics University of Exeter Stocker Road, Exeter, Devon, EX4 4QL, UK http://newton.ex.ac.uk/research/emag ______________________________ [[alternative HTML version deleted]]