Getting the mean value for every Id in a data frame

后端 未结 5 1533
旧巷少年郎
旧巷少年郎 2021-01-21 06:06

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 va

相关标签:
5条回答
  • 2021-01-21 06:46

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

    0 讨论(0)
  • 2021-01-21 07:00

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

    0 讨论(0)
  • 2021-01-21 07:01

    I heart reshape:

    cast(x, Id ~ ., mean)
    
    0 讨论(0)
  • 2021-01-21 07:08

    One possible solution using aggregate:

    aggregate(Value ~ Id, data=tmp, FUN=mean)
    
    0 讨论(0)
  • 2021-01-21 07:08

    Just for completeness basic solution is tapply:

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

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

    0 讨论(0)
提交回复
热议问题