【发布时间】:2018-10-17 14:04:28
【问题描述】:
我有一个数据框,其 id 包含不同连续时间段中的值现在我想创建一个列,它是每日数据的每周平均值。
df
id date value
1 2018-1-12 3
1 2018-1-13 4
1 2018-1-14 5
1 2018-1-15 5
1 2018-1-16 3
1 2018-1-17 5
1 2018-1-18 5
1 2018-1-19 5
2 2017-1-14 8
.
.
.
12 2016-12-10 7
我想要我的 df 是什么
df
id date value mean_week
1 2018-1-12 3 mean(7 consecutive days starting 2018-1-12 and id=1)
1 2018-1-13 4 mean(7 consecutive days starting 2018-1-12 and id=1)
1 2018-1-14 5 mean(7 consecutive days starting 2018-1-12 and id=1)
1 2018-1-15 5 mean(7 consecutive days starting 2018-1-12 and id=1)
1 2018-1-16 3 mean(7 consecutive days starting 2018-1-12 and id=1)
1 2018-1-17 5 mean(7 consecutive days starting 2018-1-12 and id=1)
1 2018-1-18 5 mean(7 consecutive days starting 2018-1-12 and id=1)
1 2018-1-19 5 NA(since there is no consecutive seven days)
2 2017-1-14 5 mean(7 consecutive days starting 2017-1-14 and id=2)
.
.
.
12 2016-12-10 7 NA(since there is no consecutive seven days)
我搜索了一种简单的方法,但到目前为止我只在循环方式中进行。
【问题讨论】:
标签: r dplyr plyr xts lubridate