[R] Calculate the latest Z-score of all zoo time series

Gabor Grothendieck ggrothendieck at gmail.com
Tue Jul 26 16:57:29 CEST 2011


On Tue, Jul 26, 2011 at 9:48 AM, thierrydb <thierrydb at gmail.com> wrote:
> Hello,
>
> I have a population of 2000+ zoo time series (but my environment also
> contains objects that are not zoo time series). I'm trying to calculate the
> latest 90 days Z-Score of all zoo time series, using the following code:
>
>
> LZS<-function(ser) {
> temp<-window(ser,start=Sys.Date()-90)
> last((temp-mean(temp))/sd(temp))
> }
>
> sapply(ls(), LZS )
>
>
> The LZS function works on individual zoo time series, but not when I try to
> use sapply to do it on the whole objects list. I guess this has to do with
> the fact that not all objects are zoo. How can I do this correctly?
>

LZS expects a series but the above code is passing it the name of the
series rather than the series itself.  Try replacing the line
referencing ser with the following which gets the series whose name is
held in ser and applies window to that:

temp<-window(get(ser),start=Sys.Date()-90)

or else pass it the series rather than its name:

lapply(lapply(ls(pattern = "zz.*"), get), LZS)



-- 
Statistics & Software Consulting
GKX Group, GKX Associates Inc.
tel: 1-877-GKX-GROUP
email: ggrothendieck at gmail.com



More information about the R-help mailing list