Check the *apply() series of functions. tapply() will do what you want.

attach(mtcars)
tapply(hp,list(cyl,gear),mean)

HTH,
Daniel





Mark Alen wrote:
> 
> I know commands like xtabs and table allow a user to do cross-tabulation
> For example the following command generates a pivot table that shows the
> number of cars that have the same number of gears and cylinders.
> 
>>xtabs(~cyl+gear,data =mtcars)
>    gear
> cyl  3 4 5
>   4 1 8 2
>   6 2 4 1
>   812 0 2
>>
> 
> We can extend the formula so it could show the sum of the horse power for
> the cars in each bin
> 
>>xtabs(hp~cyl+gear,data =mtcars)
>    gear
> cyl    3   4   5
>   4  97 608 204
>   6 215 466 175
>   82330   0 599
>>
> 
> I am now wondering, is it possible to calculate the mean of horse powers
> for cars in each bin? for example something like
> this xtabs(mean(hp)~cyl+gear, data = mtcars)
> 
> Thank you 
> Mark
> 
> 
> ______________________________________________
> R-help@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
> 

--
View this message in context: 
http://r.789695.n4.nabble.com/Calculating-the-mean-of-values-in-tables-using-formulae-tp3688416p3688437.html
Sent from the R help mailing list archive at Nabble.com.

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to