# [R] extracting row means from a list

From: Andy Bunn <abunn_at_whrc.org>
Date: Wed 20 Jul 2005 - 01:13:55 EST

foo <- list()
for(i in 1:10){

```     # The real data are read in from a series of numbered text files
foo[[i]] <- data.frame(x1 = rnorm(100), x2 = rnorm(100), x3 =
rnorm(100),
x4 = rnorm(100), x5 = rnorm(100), x6 =
```
rnorm(100))

}

str(foo)
# by hand
mean.x1 <-
rowMeans(cbind(foo[][,1],foo[][,1],foo[][,1],foo[][,1],foo[][
,1]),

```                          foo[][,1],foo[][,1],foo[][,1],foo[][,1
```
],foo[][,1]))
mean.x2 <-
rowMeans(cbind(foo[][,2],foo[][,2],foo[][,2],foo[][,2],foo[][
,2]),
```                          foo[][,2],foo[][,2],foo[][,2],foo[][,2
```
],foo[][,2]))
# and so on to column 6
mean.x6 <-
rowMeans(cbind(foo[][,6],foo[][,6],foo[][,6],foo[][,6],foo[][
,6]),
```                          foo[][,6],foo[][,6],foo[][,6],foo[][,6
```
],foo[][,6]))

I've implemented this with nested loops that create temporary variables and calc the mean, but the approach is clunky. E.g.,

# nested loops
for(i in 1:ncol(foo[])){
for(j in 1:length(foo)){
# etc ...
}
}

Is there a way to build a better mouse trap?

TIA, Andy

Thanks, Andy

R-help@stat.math.ethz.ch mailing list