[R] Looking for a sort of tapply() to data frames

Thomas Lumley tlumley at u.washington.edu
Wed Dec 14 17:21:58 CET 2005


On Wed, 14 Dec 2005, January Weiner wrote:

> Hi,
>
> I read about the by() function, but it does not seem to do the job I
> need. Here is the problem:

by() will work, you just need to use the right function in it.

You want

by(df[,-1], df$Day, function.that.means.each.column)

so all you need to do is write  function.that.means.each.column()
In this case there is a built-in function, colMeans, so you don't even 
have to write it.

More generally (eg the approach would work for medians as well)

by(df[,1], df$Day, function(today) apply(today, 2, mean))

Finally, you could just use aggregate().

 	-thomas

> Say - I have a data frame, with three columns.  The first one contains
> strings that describe the data points, with repeats (for example, days
> of a week).  The other two contain numbers. Something like that:
>
> Day val1 val2
> Tue 1    2
> Tue 2    8
> Tue 3    5
> Wed 1    2
> Wed 1    8
> etc.
>
> Now I would like to have a data frame with averages for each week:
>
> Day val1 val2
> Tue 2    5
> Wed 1    5
> etc.
> I now I can do tapply(DF$val2, DF$days, mean) to get the means for
> val2. But I would like to have a data frame as result (as in reality I
> have many more columns).
>
> Further question: where can I find a good, advanced introduction to R
> data types? R's help() function just kills my brain, and the tutorials
> are very limited.
>
> My kind regards,
>
> January Weiner
>
> --
> ------------ January Weiner 3  ---------------------+---------------
> Division of Bioinformatics, University of Muenster  |  Schloßplatz 4
> (+49)(251)8321634                                   |  D48149 Münster
> http://www.uni-muenster.de/Biologie.Botanik/ebb/    |  Germany
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>

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


More information about the R-help mailing list