[R] Mean of matrices entries in list of lists

arun smartpink111 at yahoo.com
Fri Nov 9 20:21:58 CET 2012


HI,

It is better to have a reproducible example to work with.  I hope this helps you.
library(reshape)
library(zoo)

set.seed(132)
list1<-lapply(1:13,function(x) lapply(1:12,function(x) data.frame(col1=sample(1:50,5,replace=TRUE),col2=sample(1:150,5,replace=TRUE)))) 
names(list1)<-2000:2012

nameCol<-unique(unlist(lapply(list1,function(x) lapply(x,function(x) names(x)))))
nameCol1<-nameCol[[1]]
nameCol2<-nameCol[[2]]


fun1<-function(lst,Colname,Nrows){
txt<-paste0(paste0(".*",c(paste0("0",1:9),10:12),".",Colname,"|"),collapse="")
 txt1<-gsub("(.*)\\|$","\\1",txt)
 lst2<-lapply(lapply(lapply(lapply(lst,function(x) {names(x)<-paste0(names(lst)[match.call()[[2]][[3]]],c(paste0("0",1:9),10:12),sep="")
 return(x)}),function(x) do.call(cbind,x)),function(x) x[grepl(txt1,colnames(x))]),function(x) melt(x))
res1<-melt(lapply(lst2,function(x) {x$variable3<-unlist(lapply(strsplit(as.character(as.yearmon(gsub("(.*)\\..*","\\1",x[,1]),"%Y%m"))," "),function(x) paste(x,collapse="")))
 return(x)}))
res2<-lapply(lapply(split(res1,gsub("(\\w{3}).*","\\1",res1$variable3)),function(x) {x$hour<-rep(1:Nrows,times=length(unique(x$L1)))
 return(x)}),function(x) aggregate(x$value,by=list(x$hour),mean))
res2}


fun1(list1,nameCol1,5) #Nrows: number of rows in each list element
 head(fun1(list1,nameCol1,5),2) #sorted alphabetically

Apr
#  Group.1        x
#1       1 26.00000
#2       2 32.53846
#3       3 26.23077
#4       4 28.69231
#5       5 27.15385

#$Aug
 # Group.1        x
#1       1 23.07692
#2       2 24.69231
#3       3 25.61538
#4       4 21.07692
#5       5 31.46154
A.K.








----- Original Message -----
From: Flummi <Inka.Roesel at gmx.net>
To: r-help at r-project.org
Cc: 
Sent: Friday, November 9, 2012 8:56 AM
Subject: [R] Mean of matrices entries in list of lists

Hey there,

I've got a list of lists with matrices:
A list with 13 entries (representing years), each of them another list with
12 matrices (representing one month). In each matrix there are as many rows
as there are hours in the different months and 2 columns, since two
meteorological parameters are measured.
What I want to do is to calculate the hourly mean values for each month over
the whole 13 year period

for (j in 1:12){
(list[[1]][[j]] [1,1] + list[[2]][[j]]  [1,1] + ...... list[[13]][[j]][1,1]
)/13 
}

How can I do that nicely? 


Original code looks like this (zeilen[j] is the number of hours for j=1 =
January, 2 = february and so on):


mean_list<-list()


for (j in 1:12){

mean_list[[j]]<-matrix(0,nrow=zeilen[j], ncol=2)

    for (k in 1:zeilen[j]){
    mean_list[[j]][k,1]<-
(biglist_new[[1]][[j]][k,1]+biglist_new[[2]][[j]][k,1]+biglist_new[[3]][[j]][k,1]+biglist_new[[4]][[j]][k,1]+biglist_new[[5]][[j]][k,1]+biglist_new[[6]][[j]][k,1]+biglist_new[[7]][[j]][k,1]+biglist_new[[8]][[j]][k,1]+biglist_new[[9]][[j]][k,1]+biglist_new[[10]][[j]][k,1]+biglist_new[[11]][[j]][k,1]+biglist_new[[12]][[j]][k,1]+biglist_new[[13]][[j]][k,1])/13
    mean_list[[j]][k,2]<-
(biglist_new[[1]][[j]][k,2]+biglist_new[[2]][[j]][k,2]+biglist_new[[3]][[j]][k,2]+biglist_new[[4]][[j]][k,2]+biglist_new[[5]][[j]][k,2]+biglist_new[[6]][[j]][k,2]+biglist_new[[7]][[j]][k,2]+biglist_new[[8]][[j]][k,2]+biglist_new[[9]][[j]][k,2]+biglist_new[[10]][[j]][k,2]+biglist_new[[11]][[j]][k,2]+biglist_new[[12]][[j]][k,2]+biglist_new[[13]][[j]][k,2])/13
            }
}




--
View this message in context: http://r.789695.n4.nabble.com/Mean-of-matrices-entries-in-list-of-lists-tp4649054.html
Sent from the R help mailing list archive at Nabble.com.

______________________________________________
R-help at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.





More information about the R-help mailing list