I have a data frame of daily open, high, low and settle prices. How can I aggregate this data weekly? The data frame has five columns, the first is the date column and the rest are the prices.
On Wed, 11 May 2005 15:44:41 -0400 Omar Lakkis wrote:> I have a data frame of daily open, high, low and settle prices. How > can I aggregate this data weekly?When you transform your data from data.frame to a "zoo" time series object (in package zoo) then you can use the corresponding aggregate method. See ?aggregate.zoo and the vignette in the package zoo. Also look at the archives of the finance SIG mailing list which recently discussed how to do this using the zoo or the its package. Z> The data frame has five columns, the first is the date column and the > rest are the prices. > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > R-project.org/posting-guide.html >
Assuming dfr["day","o","h","l","c"] and day like 2004-12-28: dt <- strptime(as.character(dfr$day),format="%Y-%m-%d") + 0 wk <- format(dt,"%Yw%U") aggr <- aggregate(list(dfr$o,dfr$h,dfr$l,dfr$c),list(wk),mean) colnames(aggr) <- etc -----Original Message----- From: Omar Lakkis [mailto:uofiowa at gmail.com] Sent: Wednesday, May 11, 2005 3:45 PM To: r-help at stat.math.ethz.ch Subject: [R] aggregate I have a data frame of daily open, high, low and settle prices. How can I aggregate this data weekly? The data frame has five columns, the first is the date column and the rest are the prices. ______________________________________________ R-help at stat.math.ethz.ch mailing list stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! R-project.org/posting-guide.html
In fact since you have dates and not datetimes use as.Date() instead of strptime(). On 5/11/05, bogdan romocea wrote:> Assuming dfr["day","o","h","l","c"] and day like 2004-12-28: > dt <- strptime(as.character(dfr$day),format="%Y-%m-%d") + 0 > wk <- format(dt,"%Yw%U") > aggr <- aggregate(list(dfr$o,dfr$h,dfr$l,dfr$c),list(wk),mean) > colnames(aggr) <- etc > > -----Original Message----- > From: Omar Lakkis [mailto:uofiowa at gmail.com] > Sent: Wednesday, May 11, 2005 3:45 PM > To: r-help at stat.math.ethz.ch > Subject: [R] aggregate > > I have a data frame of daily open, high, low and settle prices. How > can I aggregate this data weekly? > The data frame has five columns, the first is the date column and the > rest are the prices. > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! R-project.org/posting-guide.html >