Skip to content
Prev 59349 / 398502 Next

How to calculate the stratified means in a data frame?

On Thu, 2004-11-18 at 15:34 -0500, Frank Duan wrote:
One way is to use by(). Using the 'iris' dataset to get the means for
Sepal.Length by Species:
INDICES: setosa
[1] 5.006
------------------------------------------------------ 
INDICES: versicolor
[1] 5.936
------------------------------------------------------ 
INDICES: virginica
[1] 6.588

See ?by, also ?tapply and ?aggregate.

Note also the use of with() as a wrapper, in lieu of attach() here.

HTH,

Marc Schwartz