Skip to content

Instantly share code, notes, and snippets.

@omartinez182
Created June 19, 2020 18:08
Show Gist options
  • Save omartinez182/812f0e1dc788703d151a5a3b9a4e75c0 to your computer and use it in GitHub Desktop.
Save omartinez182/812f0e1dc788703d151a5a3b9a4e75c0 to your computer and use it in GitHub Desktop.
Understanding Time Series Analysis with R - Snippet 4
df <- data
Y <- 2 # 2 is the position of our "sales" column in the data frame
k <- 4 # k is equal to the size of the subset
#Array with all of the raw values of 'Y'
arr <- df[, Y]
#Empty array to store the values of the moving averages
MM<-rep(NA, length(arr))
index <- k-1
for(i in c(1:length(arr))){
if(i <= length(arr) -k+1){
block <- mean(arr[seq(i, i+(k-1))])
MM[index] <- block
index <- index+1
}
}
MM
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment