This may sound trivial, but I want to know how to create a loop with a xts objects. For example I have :
make<-rbind(mean(x['199901']),
mean(x['199902']),
mean(x['199903']),
mean(x['199904']),
mean(x['199905']),
mean(x['199906']))
x is an xts object and I will like to extract monthly means from this.
I think you should use period.apply, here an example to compute the mean for each month
zoo.data <- xts(rnorm(31)+10,as.Date(13514:13744,origin="1970-01-01"))
ep <- endpoints(zoo.data,'months')
period.apply(zoo.data, INDEX=ep, FUN=function(x) mean(x))
So to get this for only the 6 first months :
period.apply(zoo.data, INDEX=ep[1:6], FUN=function(x) mean(x))
there are also some wrappers of this function, like:
apply.daily(x, FUN, ...)
apply.weekly(x, FUN, ...)
apply.monthly(x, FUN, ...)
apply.quarterly(x, FUN, ...)
apply.yearly(x, FUN, ...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With