[R] Average over data sets

Ed Long e.long at ucl.ac.uk
Wed Sep 2 14:55:11 CEST 2009


Hello,

I have a number of files output1.dat, output2.dat, ... , output20.dat,  
each of which monitors several variables over a fixed number of  
timepoints. From this I want to create a data frame which contains the  
mean value between all files, for each timepoint and each variable.

The code below works, but it seems like I should be able to do the  
second part without a for loop. I played with sapply(myList, mean),  
but that seems to take the mean between time points and files, rather  
than just between files.

#Number of files to calculate mean value between
numberOfRuns = 20;
myList = list();
for (i in 1:numberOfRuns) {
	#Read in file
	fileName = paste("output", i, ".dat", sep="");
	myData = read.table(fileName, header=TRUE);
	#Append data frame to list
	myList[[i]] = myData;
}

#Create variable to store data means
myAverage = myList[[1]]/numberOfRuns;

for (i in 2:numberOfRuns) {
	myAverage = myAverage + myList[[i]]/numberOfRuns;
}

Is a list of data frames a sensible structure to store this or should  
I use an array?

Any pointers gratefully received.

Ed Long




More information about the R-help mailing list