[R] Odp: means across list of data frames

Petr PIKAL petr.pikal at precheza.cz
Tue Sep 20 18:04:56 CEST 2011


> I have a list of data frames like the following:
> set.seed(123)
> a<- data.frame(x=runif(10), y = runif(10), sample = seq(1,10))
> b<- data.frame(x=runif(10), y = runif(10), sample = seq(1,10))
> L<- list(a,b)
> All data frames in the list have the same dimensions.  I need to 
> the sample means for x and y.  The real data are lists of several 
> quite large dataframes, so I need something that is pretty fast.  Here 
> what I have so far:
> y<- rowMeans(sapply(L,'[[','y'))
> x<- rowMeans(sapply(L,'[[','x'))
> this works great, but I'm only able to get the sample means for one 
> at a time.  Is there a way to automate things a bit so that I can get 
> sample means for several columns with one call?

I am not sure if it is suitable for you but plyr package can be used. 
Maybe somebody could do better

aaply(laply(L, as.matrix), 3, colMeans)

shall give you an array with rows of means for columns of your data 


> Thanks for any suggestions,
> Matthew
>    [[alternative HTML version deleted]]
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide 
> and provide commented, minimal, self-contained, reproducible code.

More information about the R-help mailing list