[R] list structure question
Liaw, Andy
andy_liaw at merck.com
Thu Jul 1 21:27:29 CEST 2004
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
>
>
More information about the R-help
mailing list