Skip to content
Back to formatted view

Raw Message

Message-ID: <x2d5yasvze.fsf@biostat.ku.dk>
Date: 2004-11-18T21:11:49Z
From: Peter Dalgaard
Subject: How to calculate the stratified means in a data frame?
In-Reply-To: <3b91723104111812343d837fd1@mail.gmail.com>

Frank Duan <fhduan at gmail.com> writes:

> Dear R people,
> 
> 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() will do that. (help(tapply), look at the "presidents" example).

-- 
   O__  ---- Peter Dalgaard             Blegdamsvej 3  
  c/ /'_ --- Dept. of Biostatistics     2200 Cph. N   
 (*) \(*) -- University of Copenhagen   Denmark      Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)             FAX: (+45) 35327907