I am using the Rmetrics package and would like to convert a daily price time serie into a monthly one. In SPlus I could use: aggregateSeries(timeSerie, by="months",FUN=first).
Reto Baumgartner <reto.baumg <at> gmail.com> writes:> I am using the Rmetrics package and would like to convert a daily > price time serie into a monthly one. In SPlus I could use: > aggregateSeries(timeSerie, by="months",FUN=first).I wrote now my own function. But is there an easier way? #-------------------------------------------------------------------------- ## function to make monthly time series (keep last row of each month) #------------------------------------------------------------------------ makeMonthly = function(ts){ # ts must be a timeSeries (see: "http://www.rmetrics.org/Rmetrics.R") ts=sort(ts) #sort dts=rownames(ts) #get dates months=substr(dts,6,7) #read out month len=length(months) #get length of vector b=months[1:(len-1)]!=months[2:len] #see where month is changing b=c(b,TRUE) #the last row we always take mts=ts[b,] #make monthly data and return result } #--------------------------------------------------------------------------
I am not sure about specific timeseries objects, but using an ordinary vector with the values the aggregate() function works fine: monthly.means <- aggregate(timeseries, mean, by=months) months now only has to be a vector of length(timeseries) with numbers relating each entry in timeseries to a unique month. My (not very elegant) way would be to create a unique index by extracting the year and the month from a time vector and combining them into one number, so that 10.2001 would result in 200110. I am sure there are more elegant ways using timeseries objects or something similar. HTH Jannis Reto Baumgartner schrieb:> I am using the Rmetrics package and would like to convert a daily > price time serie into a monthly one. In SPlus I could use: > aggregateSeries(timeSerie, by="months",FUN=first). > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. > >
library(xts) to.monthly(x) The code is all Fortran, and is very fast. It should work just fine on most all time-series-like objects/classes, including timeSeries. Documentation in the vignette will help, as will ?to.period A wealth of functions to manipulate/test/transform time-series data is part of xts. It is compatible with most all other time-series classes, and is mostly written in C. Operations nearly as fast as atomic types, and in some cases faster, and minimize memory usage. to.period is used by many on tick-data without issue, and can support any time granularity (seconds, mins, hours, days, weeks, months, quarters, years, and arbitrary multiples of these) Additional help is on the r-sig-finance list, as well as in a variety of slides found: http://www.quantmod.com/Columbia2008/ http://www.quantmod.com/Rmetrics2008/ As well as: http://www.quantmod.com/examples/data/ HTH Jeff -- View this message in context: http://r.789695.n4.nabble.com/how-to-make-monthly-time-series-out-of-daily-tp2196591p2239578.html Sent from the R help mailing list archive at Nabble.com.