I'm working in R and I need to do a rolling mean of the last 3 values of a data set, partinioned by 3 different collumns. Each time one of the values of the collumn changes, the rolling mean should restart the rolling. The following example portrays my objective:
Material Channel client value mean
1 2 2 5 NA
1 2 2 10 NA
1 2 2 7 7,3
1 2 2 10 9
1 2 5 5 NA
1 2 5 3 NA
1 2 5 5 13
I've tried to use the rollmean function, but it doesn't partinion the dataset by collumns.
input_mes_cluster[, peso_medio:=rollmean(peso, k = 12) , by= c("cluster","material","canal", "cliente", "regiao")]
How can I get the expected result?
Thanks!