开发者

Getting the mean value for every Id in a data frame

开发者 https://www.devze.com 2023-01-17 10:07 出处:网络
Imagine I have a data frame with 2 columns IdValue 1213 323 602211 9142231 1223 11开发者_运维技巧9312

Imagine I have a data frame with 2 columns

Id    Value
12    13
32    3
6022  11
9142  231
12    23
11开发者_运维技巧9   312
...

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


One possible solution using aggregate:

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


Just for completeness basic solution is tapply:

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

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


I heart reshape:

cast(x, Id ~ ., mean)


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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消