[R] Grouping and Computing

alexander.hener@gmx.de alexander.hener at gmx.de
Tue Feb 12 17:17:31 CET 2002


hi group,

thanks to Gregory, James, Joerg & Kaspar (in alphabetical order) for their
quick and friendly answers. 
Maybe there is someone out there with a related problem, so here is a short
summary in form of an FAQ:

How do I apply functions to certain rows of a matrics or a dataframe, in
dependence of certain (other) attributes? For example, compute separately
average weights for men and women.
One can use the function "tapply()", which allows to apply a function (mean)
to attribute X (weight) ordered by attribute Z (gender): 

"tapply(X,Z,mean)"

This function gives a list, where the results (means) are given separated by
the attributed Z.

Are there any other functions to do this?
Yes, for example functions like "by()" or "aggregate()" => "by(X,Z,mean)"
"aggregate(X,Z,mean)". But "tapply()" seems to be the most popular, and one
should try to keep it simple.

If I use a complicated function, how can I avoid confusion or simply
increasy the clearity?
First, define the function separately:

"complicated.function <- function(x) ..."

Second, use "split()" to separate the matrics or dataframe called data into
groups according to attribute Z:

"groups <- split(data,data$Z)"

Finally, use "sapply()" to apply the function to the different groups:

"sapply(groups,complicated.function)"

I hope this helps, but I realise that without much experience, like in my
case, it can first be disturbing to have such an abundance of possibilities to
"do something", and second that it is very difficult to treat a topic
separately. For example, there would be much to say about lists in this context.

CU,

Alexander Hener.

-- 
GMX - Die Kommunikationsplattform im Internet.
http://www.gmx.net

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list