A rolling average smooths the most recent observations in an ordered series.

Program

Play the script to change the window size and see how the recent average moves.

rolling_average.R
window <- 
sales <- c(10, 12, 15, 18, 21)
recent <- tail(sales, window)
average <- round(mean(recent), 1)
label <- paste("avg", average, sep = ":")
cat(label, "\n", sep = "")
window <- 
sales <- c(10, 12, 15, 18, 21)
recent <- tail(sales, window)
average <- round(mean(recent), 1)
label <- paste("avg", average, sep = ":")
cat(label, "\n", sep = "")
window <- 
sales <- c(10, 12, 15, 18, 21)
recent <- tail(sales, window)
average <- round(mean(recent), 1)
label <- paste("avg", average, sep = ":")
cat(label, "\n", sep = "")
ordered series The vector order represents time order.
window `tail(sales, window)` selects the most recent observations.
smoothing `mean(recent)` summarizes the current window.