[R-sig-finance] average values for time series
Dirk Eddelbuettel
edd at debian.org
Wed Apr 27 13:54:35 CEST 2005
On 27 April 2005 at 06:37, Jonathan Q. wrote:
| New to R, currently use FAME. One of the things I like about FAME is
| that if I have a series, say the near month for a commodity, I can set
| the series property so when i view weekly data, the data is the
| average of the daily data. alternatively, for other series like stock
There are probably several ways to do that.
| prices I can have the series always display the last value (i.e.,
| monthly would take last value in month vs the average). also able to
The its package does that pretty well:
> library(its) ## verbose output suppressed here
> IBM<-priceIts("IBM",quote="Close") ## fetches from Yahoo!
> head(IBM)
IBM Close
1991-01-02 112.12
1991-01-03 112.50
1991-01-04 112.12
1991-01-07 110.25
1991-01-08 109.00
1991-01-09 106.87
> head(extractIts(IBM, find="last", period="week"))
IBM Close
1991-01-04 112.12
1991-01-11 108.12
1991-01-18 117.62
1991-01-25 122.62
1991-02-01 126.87
1991-02-08 129.50
> 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
> head(aggregate(IBMzoo, format(dates(IBM), "%Y-%m"), mean))
1991-01 115.4127
1991-02 132.2053
1991-03 123.3240
1991-04 109.6800
1991-05 104.2241
1991-06 100.8970
>
This runs aggregate() over the zoo() object where the on-the-fly factor is
computed from the underlying POSIXt date representation, and the aggregating
function, here mean, is then passed over each unique factor set.
Hope this helps, Dirk
--
Better to have an approximate answer to the right question than a precise
answer to the wrong question. -- John Tukey as quoted by John Chambers
More information about the R-sig-finance
mailing list