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?

Marek
  • 49,472
  • 15
  • 99
  • 121
abcde123483
  • 3,885
  • 4
  • 41
  • 41
  • 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

8

One possible solution using aggregate:

aggregate(Value ~ Id, data=tmp, FUN=mean)
rcs
  • 67,191
  • 22
  • 172
  • 153
3

Just for completeness basic solution is tapply:

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

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

Marek
  • 49,472
  • 15
  • 99
  • 121
2

I heart reshape:

cast(x, Id ~ ., mean)
Brandon Bertelsen
  • 43,807
  • 34
  • 160
  • 255
1

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

Shane
  • 98,550
  • 35
  • 224
  • 217
  • 3
    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,510
  • 6
  • 49
  • 72