[R] how to make monthly time series out of daily

Reto retobaumg at gmail.com
Thu May 13 17:31:24 CEST 2010


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
}

#--------------------------------------------------------------------------



More information about the R-help mailing list