[R] Basic stratification calculations

Sean Davis sdavis2 at mail.nih.gov
Fri Mar 4 14:27:59 CET 2005


On Mar 4, 2005, at 6:55 AM, Clint Harshaw wrote:

> vito muggeo wrote:
>> Hi,
>> if I understand correctly, tapply() is your friend here,
>> vito
>> dvrecko at sfu.ca wrote:
>>>
>>> Hi. I'm a student at SFU in Canada. The basic thing I want to do is
>>> calculate means of different strata. I have 2 vectors. One has the 
>>> values I
>>> want to take the means from, the other is the four strata I am 
>>> interested
>>> in. So I essentially want to break up the information vector into 
>>> the four
>>> strata and calculate four means, one for each stratum. How can I do 
>>> this in
>>> a reasonable way?
>>>
>>>
>>> Thanks very much.
>>> Dean Vrecko
>>>
>>> PS: Incidentally I forget how to see the code of functions. Does 
>>> anyone
>>> remember the command to do this?
>>>
>>> ______________________________________________
>>> R-help at 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
>>>
>
> Would split(y,f) be of some help here? And then you could find the 
> mean for each level of f:
>
> y.split <- split(y,f)
> mean(y.split$"0")
> mean(y.split$"1")
> mean(y.split$"2")
> mean(y.split$"3")

You can automate this using aggregate:

 > a <- rep(c('A','B','C','D'),25)
 > b <- rnorm(100)
 > aggregate(b,by=list(a),mean)
   Group.1          x
1       A  0.1409995
2       B -0.1524387
3       C  0.3329184
4       D  0.1354157

Sean




More information about the R-help mailing list