You're making this more complicated than it is. You have a scale
function, and you want to apply it on a rolling basis. This is exactly what rollapply
does!
scale <- function(x) {
mu <- mean(x)
sigma <- sd(x)
x <- (x - mu)/sigma
}
scaled.data <- rollapplyr(Data, 5, scale)