[R] how to get the mean of each group?

Uwe Ligges ligges at statistik.uni-dortmund.de
Sun Jun 19 15:31:25 CEST 2005


ronggui wrote:
> i have the data x.1
> 
> 
>>x.1 <- data.frame(income=runif(100)*10000,
> 
> + edu=sample(c('hs','col','none'),100,T),y=rnorm(100)*100)
> 
> and i want to get :
> 
>        income         y
> col  5526.726 -11.00956
> hs   4196.036 -10.03861
> none 4308.111 -28.69549
> 
> 5526.726 is the mean income for col, 4196.036 is the  mean income for hs......
> 
> 
> 

See ?aggregate, ?by and ?tapply

E.g.:
  aggregate(x.1[,c(1,3)], list(x.1[,2]), mean)


Uwe Liggges




More information about the R-help mailing list