Created
June 19, 2020 18:08
-
-
Save omartinez182/812f0e1dc788703d151a5a3b9a4e75c0 to your computer and use it in GitHub Desktop.
Understanding Time Series Analysis with R - Snippet 4
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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