过去3天的滚动平均值

时间:2016-12-20 07:32:33

标签: r machine-learning

如何计算每个来电者最近3天的滚动(平均值,最大值,最小值和总和),我已尝试滚动平均但未获得成功,我的样本数据如下:

  Day   Caller Total Call
   2      A       2
   7      B       1
   8      C       1
   9      A       1
  10      B       1
  11      C       2
  12      A       1
  13      B       1
  14      C       1
  15      A       2
  16      B       2
  17      C       1
  18      A       4
  19      B       4
  20      C       1
  21      A       1
  22      B       2
  23      C       1
  24      A       2
  25      B       1

所以我的预期结果如下:

 15      A       2     mean 1.33

自过去3天以来,A值为1,1,2

类似我想计算呼叫者A,B和C的总和,平均值等。

      # load packages
       require(data.table)
        require(zoo)
        callA <-as.data.table(callA)
        setkey(callA, "Caller", "Day")
        callA[, Rolling.Average := rollmean(Sum, 3, fill = NA), by = caller]

谢谢!它工作正常,但显示错误错误:当我的数据看起来像时,k&lt; = n不为TRUE:

    Day   Caller Total Call                                                                                                                    
    2   1.16E+11        1
    2   1.19E+11        2
    2   662026601483    1
   2    772221884003    1
   2    442424243639    1
   2    345503159325    2
   2    5632504865891   2
   2    45672507410322  1                                                                                       Any solution for the same, I feel my Caller variable have some problem, I have converted to caller variable as character still it showed error 
  Error: k <= n is not TRUE

2 个答案:

答案 0 :(得分:0)

我们可以INSERT INTO student (Student_id,password,firstname)VALUES ('" & Text1.Text &" ' , CONVERT( NVARCHAR (32),HashBytes ('MD5' ,'"&Text2.Text&"' ),2),'"&Text3.Text&"')" group_by为每个组Caller选择最后三行,并分别计算其tailsum

mean

答案 1 :(得分:0)

您希望索引右对齐而不是居中吗?在那种情况下试试

require(data.table)
require(zoo)
callA <- fread("Day   Caller TotalCall
   2      A       2
   7      B       1
   8      C       1
   9      A       1
  10      B       1
  11      C       2
  12      A       1
  13      B       1
  14      C       1
  15      A       2
  16      B       2
  17      C       1
  18      A       4
  19      B       4
  20      C       1
  21      A       1
  22      B       2
  23      C       1
  24      A       2
  25      B       1")
setkey(callA, "Caller", "Day")
callA[, Rolling.Average := rollmeanr(TotalCall, 3, fill = NA), by = Caller]
callA
#     Day Caller TotalCall Rolling.Average
#  1:   2      A         2              NA
#  2:   9      A         1              NA
#  3:  12      A         1        1.333333
#  4:  15      A         2        1.333333 # <====
#  5:  18      A         4        2.333333
#  6:  21      A         1        2.333333
#  7:  24      A         2        2.333333
#  8:   7      B         1              NA
#  9:  10      B         1              NA
# 10:  13      B         1        1.000000
# 11:  16      B         2        1.333333
# 12:  19      B         4        2.333333
# 13:  22      B         2        2.666667
# 14:  25      B         1        2.333333
# 15:   8      C         1              NA
# 16:  11      C         2              NA
# 17:  14      C         1        1.333333
# 18:  17      C         1        1.333333
# 19:  20      C         1        1.000000
# 20:  23      C         1        1.000000