[R] Average over data sets

jim holtman jholtman at gmail.com
Wed Sep 2 15:15:36 CEST 2009


Why not create a single data frame with all the results and then you
can easily use tapply/sapply/aggregate/reshape package

# combine into a single DF
combinedDF <- do.call(rbind, MyList)


On Wed, Sep 2, 2009 at 8:55 AM, Ed Long<e.long at ucl.ac.uk> wrote:
> 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
>
> ______________________________________________
> 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.
>



-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem that you are trying to solve?




More information about the R-help mailing list