[R] How to calculate the stratified means in a data frame?
Prof Brian Ripley
ripley at stats.ox.ac.uk
Thu Nov 18 22:07:20 CET 2004
On Thu, 18 Nov 2004, Frank Duan wrote:
> I have a simple question to ask. Suppose I have a data.frame with two
> variables: one factor (x) and one numeric (y), I want to calculate the
> mean of y for each value of x. Although it's easy to do it within a
> for a loop, I believe there may be a concise way by using some kinds
> of "apply" functions. Could anyone tell me how to do that? Thank you.
tapply(y, x, mean) # which _is_ in `An Introduction to R', BTW
?by
?aggregate
for more sophisticated packaging of such ideas.
--
Brian D. Ripley, ripley at stats.ox.ac.uk
Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/
University of Oxford, Tel: +44 1865 272861 (self)
1 South Parks Road, +44 1865 272866 (PA)
Oxford OX1 3TG, UK Fax: +44 1865 272595
More information about the R-help
mailing list