R 将每个ID的每日值汇总为每周平均值

R 将每个ID的每日值汇总为每周平均值,r,dplyr,plyr,xts,lubridate,R,Dplyr,Plyr,Xts,Lubridate,我有一个带有ID的dataframe,其中包含独立连续时间段中的值,现在我想创建一个列,它是每日数据的每周平均值 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 20

我有一个带有ID的dataframe,其中包含独立连续时间段中的值,现在我想创建一个列,它是每日数据的每周平均值

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)

我寻找了一个简单的方法,但到目前为止,我只是以循环的方式来做。

类似这样的事情,但我不了解周开始条件

library(tidyverse)
 df=read.table(text="id   date      value
  1   2018-1-12 3
               1   2018-1-13 4
               1   2018-1-14 5
               1   2018-1-16 3
               1   2018-1-17 5",header=T)

 library(lubridate)
 df%>%
   mutate(week=isoweek(date))%>%
   group_by(week,id)%>%
   mutate(mean_week=mean(value,na.rm = T))
# A tibble: 5 x 5
# Groups:   week, id [2]
     id date      value  week mean_week
  <int> <fct>     <int> <dbl>     <dbl>
1     1 2018-1-12     3    2.        4.
2     1 2018-1-13     4    2.        4.
3     1 2018-1-14     5    2.        4.
4     1 2018-1-16     3    3.        4.
5     1 2018-1-17     5    3.        4.
库(tidyverse)
df=读取。表格(text=“id日期值
1   2018-1-12 3
1   2018-1-13 4
1   2018-1-14 5
1   2018-1-16 3
1 2018-1-17 5“,收割台=T)
图书馆(lubridate)
df%>%
变异(周=等周(日期))%>%
分组依据(周,id)%>%
变异(平均周=平均值,na.rm=T))
#一个tibble:5x5
#分组:周,id[2]
id日期值周平均值周
1     1 2018-1-12     3    2.        4.
2     1 2018-1-13     4    2.        4.
3     1 2018-1-14     5    2.        4.
4     1 2018-1-16     3    3.        4.
5     1 2018-1-17     5    3.        4.

每周汇总分组的数据。但是使用
mutate()
这样每一行都会得到汇总值

df <- data.frame(date = as.Date("2018-01-01")+1:100,
                 value = sample(1:10,size = 100,replace = TRUE))


require(dplyr)
require(lubridate)



df %>% mutate(week = week(date)) %>%
  group_by(week) %>%
  mutate(summary = paste(round(mean(value),1),"(",n()," consecutive days starting ",min(date),")"))
df%变异(周=周(日期))%>%
按(周)分组%>%
变异(摘要=粘贴(四舍五入(平均值,1),“(”,n(),“连续天数开始”,分钟(日期),”))
给予

日期值周摘要
1 2018-01-02 3 1 4.7(从2018-01-02开始连续6天)
2 2018-01-03 6 1 4.7(从2018-01-02开始连续6天)
3 2018-01-04 11 4.7(从2018-01-02开始连续6天)
4 2018-01-05 11 4.7(从2018-01-02开始连续6天)
5 2018-01-06 10 1 4.7(从2018-01-02开始连续6天)
6 2018-01-07 7 1 4.7(从2018-01-02开始连续6天)
7 2018-01-08 22 4(从2018-01-08开始连续7天)
8 2018-01-09 22 4(从2018-01-08开始连续7天)
9 2018-01-10 5 2 4(从2018-01-08开始连续7天)
10 2018-01-11 7 2 4(从2018-01-08开始连续7天)

谢谢@jyjek的回复。。。编辑我的问题,使我的需求更容易理解。
date value  week                                           summary
<date> <int> <dbl>                                             <chr>
1  2018-01-02     3     1 4.7  ( 6  consecutive days starting  2018-01-02 )
2  2018-01-03     6     1 4.7  ( 6  consecutive days starting  2018-01-02 )
3  2018-01-04     1     1 4.7  ( 6  consecutive days starting  2018-01-02 )
4  2018-01-05     1     1 4.7  ( 6  consecutive days starting  2018-01-02 )
5  2018-01-06    10     1 4.7  ( 6  consecutive days starting  2018-01-02 )
6  2018-01-07     7     1 4.7  ( 6  consecutive days starting  2018-01-02 )
7  2018-01-08     2     2   4  ( 7  consecutive days starting  2018-01-08 )
8  2018-01-09     2     2   4  ( 7  consecutive days starting  2018-01-08 )
9  2018-01-10     5     2   4  ( 7  consecutive days starting  2018-01-08 )
10 2018-01-11     7     2   4  ( 7  consecutive days starting  2018-01-08 )