Reputation: 10984
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
8 12 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
8 2330 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)
Upvotes: 7
Views: 5891
Reputation: 10984
Another way of calculating it is by using the aggregate() function. Although the output is not in the form of a table. (via twitter)
> aggregate(hp~cyl+gear,data=mtcars,mean)
cyl gear hp
1 4 3 97.0000
2 6 3 107.5000
3 8 3 194.1667
4 4 4 76.0000
5 6 4 116.5000
6 4 5 102.0000
7 6 5 175.0000
8 8 5 299.5000
>
Upvotes: 0
Reputation: 55695
You can do it in one line using cast
from the reshape
library
cast(mtcars, cyl ~ gear, value = 'hp', fun = mean)
Upvotes: 10
Reputation: 30495
(Moving my comment to a response, so I can better edit it.)
I'm not sure how to do it with xtabs
(which I've never used before), but here are a couple of ways of doing it using the reshape
and plyr
packages.
> x = melt(mtcars, id = c("cyl", "gear"), measure = c("hp"))
> cast(x, cyl ~ gear, mean)
> x = ddply(mtcars, .(cyl, gear), summarise, hp = mean(hp))
> cast(x, cyl ~ gear)
Upvotes: 3
Reputation: 10984
One interesting response that I received from r-help is as following:
> attach(mtcars)
> tapply(hp,list(cyl,gear),mean)
3 4 5
4 97.0000 76.0 102.0
6 107.5000 116.5 175.0
8 194.1667 NA 299.5
>
Upvotes: 7