4

I have ten huge lists(each list has seven element but elements are huge) and I need to calculate the element wise mean of these lists. So if there are A1, A2, A3,..., A10 lists. I need to calculate :

mean1 = mean(A1[[1]], A2[[1]], A3[[1]], ...,A10[[1]])
.
.
.
mean7 = mean(A1[[7]], A2[[7]], A3[[7]], ....A10[[7]])

I have done it with for loop but I wanted to know if there is a better solution provided by R. Thank you in advance.

hora
  • 845
  • 5
  • 14
  • 25

2 Answers2

7

If your A[[·]] are vectors as the following list,

> ( List <- list(A=1:4, B=5:8, C=9:12) )
$A
[1] 1 2 3 4

$B
[1] 5 6 7 8

$C
[1]  9 10 11 12

then you can use this approach to obtain the mean:

> rowMeans(simplify2array(List))
[1] 5 6 7 8

rowMeans(as.data.frame(List)) will give you the same result.

Jilber Urbina
  • 58,147
  • 10
  • 114
  • 138
  • Yes, and similar to what I was just about to try: `A1 <- 1:10; A2 <- 21:20; A3 <- 21:30; rowMeans(do.call(cbind,list(A1,A2,A3)))` ... :) – texb Jul 23 '13 at 16:27
  • nice use of `simplify2array` function. it seems to be `matrix(unlist(.., use.names=FALSE), nrow=..)` under the hood. – Arun Jul 23 '13 at 17:17
3

Assuming your As are lists of vectors:

Anames <- paste0("A", 1:10)

# for completeness
for(A in Anames)
    assign(A, lapply(1:7, function(x) rnorm(1000)))

sapply(1:7, function(i)
{
    m <- sapply(Anames, function(A) get(A)[[i]])
    mean(m)
})

This avoids building a copy of all your As in memory, instead retrieving them one at a time and extracting the desired vector. But if you have enough memory to store all that data, you could probably afford to store a copy as well.

Hong Ooi
  • 56,353
  • 13
  • 134
  • 187
  • :D Did I ask in a weird way? yes I have enough memory on the cluster, I am checking if the solution works or not but it takes time. – hora Jul 23 '13 at 16:45
  • 2
    @hora I think it would have saved some confusion if you'd just said "10 huge dataframes" as opposed to "10 huge lists". – Hong Ooi Jul 23 '13 at 17:28