[R-sig-finance] average values for time series

Achim Zeileis Achim.Zeileis at wu-wien.ac.at
Wed Apr 27 14:00:09 CEST 2005


Dirk,

thanks for pointing out how to do this in zoo, but:

> > head(extractIts(IBM, find="last", period="month"))
>            IBM Close
> 1991-01-31    126.75
> 1991-02-28    128.75
> 1991-03-28    113.87
> 1991-04-30    103.00
> 1991-05-31    106.12
> 1991-06-28     97.12
> 
> I just taught myself how to the other part with zoo -- pretty slick:
> 
> > library(zoo)				## once again verbose output nixed
> > IBMzoo <- zoo(IBM)			## turn its object into zoo object

here, you need 

R> IBMzoo <- as.zoo(IBM)

otherwise the time index is not preserved. And then you can do

R> head(aggregate(IBMzoo, format(dates(IBM), "%Y-%m"), mean))

to get means or if you want the last observation, you can do:

R> last <- function(x) tail(x, 1)
R> head(aggregate(IBMzoo, format(dates(IBM), "%Y-%m"), last))
              
1991-01 126.75
1991-02 128.75
1991-03 113.87
1991-04 103.00
1991-05 106.12
1991-06  97.12

or you can use any other aggregation function, you would like.
Z



More information about the R-sig-finance mailing list