Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get xtabs to calculate means instead of sums in R?

Tags:

r

average

mean

I have a data frame where each line represents an individual. That data frame has two variables: age and year. I want to make a table of average ages per year. How can I do it?

The best I could come up with was xtabs(age ~ year, dataframe), but this gives me the sum of ages per year.

like image 352
Waldir Leoncio Avatar asked Jan 25 '12 18:01

Waldir Leoncio


3 Answers

Use aggregate:

xtabs(hp~cyl+gear,aggregate(hp~cyl+gear,mtcars,mean))
   gear
cyl        3        4        5
  4  97.0000  76.0000 102.0000
  6 107.5000 116.5000 175.0000
  8 194.1667   0.0000 299.5000
like image 83
James Avatar answered Nov 09 '22 23:11

James


Have a look at the plyr package, specifically ddply

ddply(dataframe, .(year), summarise, mean(age))

If you have to use xtabs... see the above answer.

like image 34
Justin Avatar answered Nov 09 '22 22:11

Justin


I'm glad that xtabs solution works for you. I must have a package loaded that interferes with it for me (and gives an error). Another solution would be:

tapply(dfrm$age, dfrm$year, FUN=mean)

To get additional dimensions to the table (array) just keep adding additional factors to the second INDEX argument in the form list(fac1, fac2, fac3).

Applied to the example using mtcars:

 tapply(mtcars$hp, list(mtcars$cyl,mtcars$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

Or even more compactly:

with(mtcars, tapply(hp, list(cyl, gear), mean))
like image 37
IRTFM Avatar answered Nov 09 '22 23:11

IRTFM