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

From: Thomas Lumley <tlumley_at_u.washington.edu>
Date: Thu 15 Dec 2005 - 03:21:58 EST

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@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@u.washington.edu	University of Washington, Seattle

______________________________________________

R-help@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 Received on Thu Dec 15 03:38:18 2005

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:41:39 EST