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? Many thanks, TDB -- View this message in context: http://r.789695.n4.nabble.com/Calculate-the-latest-Z-score-of-all-zoo-time-series-tp3695820p3695820.html Sent from the R help mailing list archive at Nabble.com.
The ls() function return the names of the objects not the objects. So if you have : a <- c(1,2,3) b <- c('A','B','C') ls() will return : "a" "b" which only 2 letters. ----- Christophe Poulet GIGA-Research. Human Genetics Dept. Li?ge, Belgium. -- View this message in context: http://r.789695.n4.nabble.com/Calculate-the-latest-Z-score-of-all-zoo-time-series-tp3695820p3695916.html Sent from the R help mailing list archive at Nabble.com.
Gabor Grothendieck
2011-Jul-26 14:57 UTC
[R] Calculate the latest Z-score of all zoo time series
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