2017-07-13 117 views
0

我有數據幀:滾動平均值與rcpproll

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誤差(。數據,點): 不相容尺寸(12),期望13(組大小)或1

回答

0

林找到一個解決方案。如果您遇到同樣的問題,請使用:

library(dplyr) 
library(RcppRoll) 

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