# Rolling z-score function in R

I have written a rolling z-score function myself. Is my implementation of rolling z-score function correct?

#x is an xts object and y duration eg rollz(x,25)
rollz<-function(x,y){
avg=rollapply(x, y, mean)
std=rollapply(x, y, sd)
z=(x-avg)/std
return(z)
}


EDIT

Here is the description of z-score. A rolling (moving) z-score calculates z-score at a particular width like how we calculate simple moving averages.

• Welcome to Code Review. You may want to add a link to inform reviews of what a "rolling z-score" is, seems like a pretty specialized term. – Phrancis Jul 15 '16 at 5:04
• I have edited the question and added more information – Eka Jul 15 '16 at 5:12
• Excellent. Hope you get some good answers! – Phrancis Jul 15 '16 at 5:13
• What's an xts object ? – Siobhan Jul 20 '16 at 23:20
• its a time series object. I am passing data with time series as the index – Eka Jul 21 '16 at 1:43