Hi, I have a list in which element is a vector (all of the same length and all numeric). I want to find the mean of the first elements of the vectors, the mean of the second elements of the vectors and so on. Currently I convert the list to a data.frame and apply rowMeans(). But is there a way to to do this directly on the list? I seem to recall a post in which there was such a function (or expression) but I just cant seem to find it. Could somebody point me in the right direction? ------------------------------------------------------------------- Rajarshi Guha <rxg218 at psu.edu> <http://jijo.cjb.net> GPG Fingerprint: 0CCA 8EE2 2EEB 25E2 AB04 06F7 1BB9 E634 9B87 56EE ------------------------------------------------------------------- Despite the high cost of living, it remains popular.
?mapply, but I think what you are doing is as good as anything. On Thu, 1 Jul 2004, Rajarshi Guha wrote:> I have a list in which element is a vector (all of the same length and > all numeric). I want to find the mean of the first elements of the > vectors, the mean of the second elements of the vectors and so on. > > Currently I convert the list to a data.frame and apply rowMeans(). But > is there a way to to do this directly on the list? I seem to recall a > post in which there was such a function (or expression) but I just cant > seem to find it. > > Could somebody point me in the right direction?-- 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
Here are three solutions but I think the original idea of just converting to a data frame and using rowMeans (last solution) is simplest: L <- list(1:5, 6:10) # test list do.call("mapply", c(sum,L)) / length(L) sapply(seq(along=L),function(i)mean(sapply(L,"[[",i))) rowMeans(as.data.frame(L)) Rajarshi Guha <rxg218 <at> psu.edu> writes: : : Hi, : I have a list in which element is a vector (all of the same length and : all numeric). I want to find the mean of the first elements of the : vectors, the mean of the second elements of the vectors and so on. : : Currently I convert the list to a data.frame and apply rowMeans(). But : is there a way to to do this directly on the list? I seem to recall a : post in which there was such a function (or expression) but I just cant : seem to find it. : : Could somebody point me in the right direction?
Here's a test:> n <- 1e4 > m <- 100 > x <- lapply(1:m, function(...) rnorm(n)) > gc(); system.time(ans1 <- rowMeans(as.data.frame(x)))used (Mb) gc trigger (Mb) Ncells 432336 23.1 818163 43.7 Vcells 1125819 8.6 5012280 38.3 [1] 7.43 0.03 7.61 0.00 0.00> gc(); system.time(ans2 <- rowMeans(do.call("cbind", x)))used (Mb) gc trigger (Mb) Ncells 442338 23.7 818163 43.7 Vcells 1155810 8.9 6821883 52.1 [1] 0.03 0.00 0.03 0.00 0.00> all(ans1 == ans2)[1] TRUE HTH, Andy> From: Rajarshi Guha > > Hi, > I have a list in which element is a vector (all of the same > length and > all numeric). I want to find the mean of the first elements of the > vectors, the mean of the second elements of the vectors and so on. > > Currently I convert the list to a data.frame and apply rowMeans(). But > is there a way to to do this directly on the list? I seem to recall a > post in which there was such a function (or expression) but I > just cant > seem to find it. > > Could somebody point me in the right direction? > > ------------------------------------------------------------------- > Rajarshi Guha <rxg218 at psu.edu> <http://jijo.cjb.net> > GPG Fingerprint: 0CCA 8EE2 2EEB 25E2 AB04 06F7 1BB9 E634 9B87 56EE > ------------------------------------------------------------------- > Despite the high cost of living, it remains popular. > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html > >