根据R中各组内的每日数据计算月平均值

时间:2018-11-27 21:38:16

标签: r

我正在尝试根据数据中每个组的每日数据计算每月平均值。在此示例中,有3个组,每个组从2011年1月1日到2011年3月31日每天进行观察

times <- c(31,28,31)
date = rep(seq(as.Date('2011-01-01'),as.Date('2011-03-31'),by = 1),
       times=3)
id = rep(rep(1001:1003, times), each=3)
val = rnorm(length(id), mean=5, sd=2)
df <- data.frame(date, id, val)

> head(df)
  date   id      val
1 2011-01-01 1001 6.341471
2 2011-01-02 1001 4.353585
3 2011-01-03 1001 8.131239
4 2011-01-04 1001 3.761434
5 2011-01-05 1001 6.344846
6 2011-01-06 1001 7.068889   


> tail(df)
      date   id      val
265 2011-03-26 1003 5.644132
266 2011-03-27 1003 4.949719
267 2011-03-28 1003 4.490786
268 2011-03-29 1003 1.739529
269 2011-03-30 1003 2.250610
270 2011-03-31 1003 1.853057

所需的输出应该是这样,并带有计算出的每月值:

monthYear  id    monthlyValue
2011-01    1001  ?
2011-02    1001  ?
2011-03    1001  ?
....       ....  ..
2011-01    1003  ?
2011-02    1003  ?
2011-03    1003  ?

1 个答案:

答案 0 :(得分:0)

> output <- aggregate(df$val, list(format(df$date, "%Y-%m"), df$id), mean)
> colnames(output) <- c('monthYear', 'id', 'monthlyValue')
> print(output)
  monthYear   id monthlyValue
1   2011-01 1001     5.368910
2   2011-02 1001     4.701553
3   2011-03 1001     5.225284
4   2011-01 1002     5.117631
5   2011-02 1002     4.869240
6   2011-03 1002     4.595431
7   2011-01 1003     5.336175
8   2011-02 1003     5.438803
9   2011-03 1003     4.658504