tags:

views:

76

answers:

6

Imagine I have a data frame with 2 columns

Id    Value
12    13
32    3
6022  11
9142  231
12    23
119   312
...

and I want to get the mean value for each "Id". Do you know of any fast way of doing this?

+3  A: 

One possible solution using aggregate:

aggregate(Value ~ Id, data=tmp, FUN=mean)
rcs
A: 

OK I figured it out. Using the plyr library and the ddply function.

ulvund
+2  A: 

Beyond aggregate, other options include by and ddply (in plyr).

Shane
+1  A: 

Also by will do the job, yet the output will be tricky.

mbq
+2  A: 

I heart reshape:

cast(x, Id ~ ., mean)
Brandon Bertelsen
+1  A: 

Just for completeness basic solution is tapply:

tapply(data$Value, data$Id, mean)

(or using with as with(data, tapply(Value, Id, mean)))

Marek