I tried a date by date forecast of a time series and it seems to be
too wild. How can I aggregate the date into weeks or months as
required?
Thanks.
The input looks like
ID datadate("YYYY-MM-DD") value_for_day
-- ----- -------
-- ------ --------
and I want to be able to change it to
ID dataweek value_for_week
or
ID datamonth value_ for_ month
aggredating date data
4 messages · analyst41 at hotmail.com, Jannis, Gene Leynes +1 more
?aggregate I would transfer your date character sting into a date object (as.POSIXct) and then extract month or week numbers (?format) from this vector and use them as indices for the aggregate function. There may be more elegant ways though.... HTH Jannis --- analyst41 at hotmail.com <analyst41 at hotmail.com> schrieb am Mi, 12.1.2011:
Von: analyst41 at hotmail.com <analyst41 at hotmail.com>
Betreff: [R] aggredating date data
An: r-help at r-project.org
Datum: Mittwoch, 12. Januar, 2011 20:20 Uhr
I tried a date by date forecast of a
time series and it seems to be
too wild.? How can I aggregate the date into weeks or
months as
required?
Thanks.
The input looks like
ID datadate("YYYY-MM-DD")? value_for_day
--? ???-----? ? ? ?
? ? ? ? ? ? ? ?
? ? ? ? -------
--? ???------? ? ?
? ? ? ? ? ? ? ?
? ? ? ???--------
and I want to be able to change it to
ID dataweek value_for_week
or
ID datamonth value_ for_ month
______________________________________________ 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.
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20110112/029a6b02/attachment.pl>
On Wed, Jan 12, 2011 at 7:35 PM, Gene Leynes <gleynes+r at gmail.com> wrote:
I like the zoo package, and there are several helpful examples.
library(zoo)
You can easily convert your data into a zoo object using
I was actually just doing this using this function:
LoadReturnData=function(x){
? ?ret = read.csv(x)
? ?ret = zoo(ret[ , -1], as.Date(ret[ , 1]))
? ?colnames(ret) = toupper(colnames(ret))
? ?return(ret)
}
fnd = LoadReturnData('/Data/SomeSpecialData.csv')
My data is already in weeks, and aggregating to months is easy using
as.yearmon
MonthIndex=as.yearmon(index(fnd))
aggregate(.~MonthIndex, data=fnd, sum)
If you have daily data and you need weeks, then you'll have to create a
vector to indicate the week, like the MonthIndex above.
e.g. for 365 days
WeekIndex = rep(1:53, each=7, length.out=365)
Also note the existence of read.zoo and its aggregate= argument.
Statistics & Software Consulting GKX Group, GKX Associates Inc. tel: 1-877-GKX-GROUP email: ggrothendieck at gmail.com