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
}
#--------------------------------------------------------------------------
______________________________________________
[email protected] 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.