moving average of he column. if there are not enough records, it will take average whatever it can. i.e. if period_cound=5 but data only has 3 values it will take average of three. don't forget to sort the data frame. most probably based on date
Arguments
- value
column name to take average
- period_count
average period count
- align
average from past="right" average from future="left". default is right.
- gap
timelag between avg value vs row. i.e. if you want to avg with one day lag, type gap=1. default is 1.
Examples
exampleDataR= data.table::as.data.table(exampleDataR)
exampleDataR = exampleDataR[order(location,item,date)]
exampleDataR[,ma5:= maR(sales,4),list(location,item)]
#> location item date sales ma5
#> 1: ankara tea 4/23/1920 22 NA
#> 2: ankara tea 4/24/1920 30 22.00000
#> 3: ankara tea 4/25/1920 29 26.00000
#> 4: ankara tea 4/26/1920 34 27.00000
#> 5: ankara tea 4/27/1920 9 28.75000
#> 6: ankara tea 4/28/1920 15 25.50000
#> 7: ankara tea 4/29/1920 40 21.75000
#> 8: ankara tea 4/30/1920 7 24.50000
#> 9: ankara tea 5/1/1920 26 17.75000
#> 10: ankara tea 5/10/1920 39 22.00000
#> 11: ankara tea 5/11/1920 39 28.00000
#> 12: ankara tea 5/12/1920 12 27.75000
#> 13: ankara tea 5/13/1920 2 29.00000
#> 14: ankara tea 5/14/1920 29 23.00000
#> 15: ankara tea 5/15/1920 7 20.50000
#> 16: ankara tea 5/16/1920 0 12.50000
#> 17: ankara tea 5/17/1920 35 9.50000
#> 18: ankara tea 5/18/1920 34 17.75000
#> 19: ankara tea 5/19/1920 20 19.00000
#> 20: ankara tea 5/2/1920 25 22.25000
#> 21: ankara tea 5/20/1920 2 28.50000
#> 22: ankara tea 5/21/1920 30 20.25000
#> 23: ankara tea 5/22/1920 30 19.25000
#> 24: ankara tea 5/3/1920 30 21.75000
#> 25: ankara tea 5/4/1920 0 23.00000
#> 26: ankara tea 5/5/1920 1 22.50000
#> 27: ankara tea 5/6/1920 4 15.25000
#> 28: ankara tea 5/7/1920 35 8.75000
#> 29: ankara tea 5/8/1920 20 10.00000
#> 30: ankara tea 5/9/1920 28 15.00000
#> 31: istanbul coffee 10/29/1923 27 NA
#> 32: istanbul coffee 10/30/1923 31 27.00000
#> 33: istanbul coffee 10/31/1923 31 29.00000
#> 34: istanbul coffee 11/1/1923 22 29.66667
#> 35: istanbul coffee 11/10/1923 6 27.75000
#> 36: istanbul coffee 11/11/1923 37 22.50000
#> 37: istanbul coffee 11/12/1923 22 24.00000
#> 38: istanbul coffee 11/13/1923 32 21.75000
#> 39: istanbul coffee 11/14/1923 25 24.25000
#> 40: istanbul coffee 11/15/1923 22 29.00000
#> 41: istanbul coffee 11/16/1923 20 25.25000
#> 42: istanbul coffee 11/17/1923 3 24.75000
#> 43: istanbul coffee 11/18/1923 39 17.50000
#> 44: istanbul coffee 11/19/1923 26 21.00000
#> 45: istanbul coffee 11/2/1923 3 22.00000
#> 46: istanbul coffee 11/20/1923 21 17.75000
#> 47: istanbul coffee 11/21/1923 27 22.25000
#> 48: istanbul coffee 11/22/1923 1 19.25000
#> 49: istanbul coffee 11/23/1923 10 13.00000
#> 50: istanbul coffee 11/24/1923 33 14.75000
#> 51: istanbul coffee 11/25/1923 23 17.75000
#> 52: istanbul coffee 11/26/1923 28 16.75000
#> 53: istanbul coffee 11/27/1923 6 23.50000
#> 54: istanbul coffee 11/3/1923 28 22.50000
#> 55: istanbul coffee 11/4/1923 30 21.25000
#> 56: istanbul coffee 11/5/1923 30 23.00000
#> 57: istanbul coffee 11/6/1923 35 23.50000
#> 58: istanbul coffee 11/7/1923 32 30.75000
#> 59: istanbul coffee 11/8/1923 2 31.75000
#> 60: istanbul coffee 11/9/1923 9 24.75000
#> location item date sales ma5
head(exampleDataR,15)
#> location item date sales ma5
#> 1: ankara tea 4/23/1920 22 NA
#> 2: ankara tea 4/24/1920 30 22.00
#> 3: ankara tea 4/25/1920 29 26.00
#> 4: ankara tea 4/26/1920 34 27.00
#> 5: ankara tea 4/27/1920 9 28.75
#> 6: ankara tea 4/28/1920 15 25.50
#> 7: ankara tea 4/29/1920 40 21.75
#> 8: ankara tea 4/30/1920 7 24.50
#> 9: ankara tea 5/1/1920 26 17.75
#> 10: ankara tea 5/10/1920 39 22.00
#> 11: ankara tea 5/11/1920 39 28.00
#> 12: ankara tea 5/12/1920 12 27.75
#> 13: ankara tea 5/13/1920 2 29.00
#> 14: ankara tea 5/14/1920 29 23.00
#> 15: ankara tea 5/15/1920 7 20.50