Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calculating the mean of values in tables using formulae [R]

Tags:

r

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)

like image 285
Mark Avatar asked Jul 23 '11 04:07

Mark


2 Answers

You can do it in one line using cast from the reshape library

cast(mtcars, cyl ~ gear, value = 'hp', fun = mean)
like image 65
Ramnath Avatar answered Sep 23 '22 02:09

Ramnath


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
> 
like image 42
Mark Avatar answered Sep 22 '22 02:09

Mark