[R] Grouping of data frames to calculate means

Thomas Lumley tlumley at u.washington.edu
Mon Apr 6 12:02:20 CEST 2009


On Mon, 6 Apr 2009, Steve Murray wrote:

>
> Dear R Users,
>
> I have 120 data frames of the format table_198601, table_198602... 
> table_198612, table_198701, table_198702... table_198712 through to 
> table_199512 (ie. the first 4 digits are years which vary from 1986 to 
> 1995, and the final two digits are months which vary from 01 to 12 for 
> each year).
>
> I simply hope to find the means of column 3 of each of the 120 tables 
> without having to type out mean(table_198601[3]) etc etc each time. How 
> would I go about doing this? And how would I go about finding the mean 
> of all the January months (01) from say 1986 to 1990?

Put all the tables in a list, then you can iterate over them, eg
  lapply(the_list, function(table) mean(table[,3))

You might also want a list of lists to maintain the annual structure, so 
that

januaries <- lapply(list_list, function(year) mean(year[[1]][,3]))

 	-thomas


> Finally, I hope to be able to plot (as a scatter graph) the values of 
> column 1 against the mean of those from column 3 for all the months in 
> the period 1989 to 1990 and then 1991 to 1995.
>
> Any help offered would be very much appreciated.
>
> Thanks,
>
> Steve
>
> _________________________________________________________________
> [[elided Hotmail spam]]
>
> ______________________________________________
> 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.
>

Thomas Lumley			Assoc. Professor, Biostatistics
tlumley at u.washington.edu	University of Washington, Seattle




More information about the R-help mailing list