3

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?

Marek
  • 47,613
  • 13
  • 96
  • 119
abcde123483
  • 3,799
  • 4
  • 38
  • 40
  • Possible duplicate of [How to average columns based on ID in R?](https://stackoverflow.com/questions/32487367/how-to-average-columns-based-on-id-in-r) – M-- Jul 25 '17 at 21:29

5 Answers5

7

One possible solution using aggregate:

aggregate(Value ~ Id, data=tmp, FUN=mean)
rcs
  • 64,778
  • 22
  • 167
  • 150
2

I heart reshape:

cast(x, Id ~ ., mean)
Brandon Bertelsen
  • 42,225
  • 33
  • 153
  • 250
2

Just for completeness basic solution is tapply:

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

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

Marek
  • 47,613
  • 13
  • 96
  • 119
1

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

Shane
  • 95,736
  • 34
  • 221
  • 217
  • 2
    I think you know that this would be considered as a low quality answer. I'd appreciate if you would edit and include some codes. Cheers. – M-- Jul 25 '17 at 21:28
0

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

mbq
  • 18,286
  • 6
  • 47
  • 72