rcpproll的滚动平均值

时间:2017-07-13 13:28:37

标签: r moving-average

我有数据框:

email    date_ts   total   duration

email1 2017-02-15  98.80    31990
email1 2017-02-16  59.52    60622
email1 2017-02-17  72.93    98105
email2 2017-02-18  54.45    29293
email2 2017-02-19  36.86    20157
email2 2017-02-25  18.66     7815
email2 2017-03-03   4.61     2407
email2 2017-03-04 180.91   182524
email3 2017-03-05  16.13     7121
email3 2017-03-24  25.65     2412

依旧......

我需要用库RcppRoll计算移动平均值。我试试:

df <- df%>%group_by(email)%>%mutate(avg = roll_mean(total, 2, na.rm=TRUE, align="right", normalize = TRUE))%>%ungroup()

但有错误:mutate_impl(.data,dots)出错:   不相容的大小(12),期望13(组大小)或1

1 个答案:

答案 0 :(得分:0)

我找到了解决方案。如果您遇到同样的问题,请使用:

library(dplyr)
library(RcppRoll)

df <- df%>%
  group_by(email)%>%
  mutate(avg = roll_mean(total, 2, na.rm=TRUE, align="right", fill = NA))%>%
  ungroup()