I have a data frame with the following information day hour volume 1 2003-07-18 10 836700 2 2003-07-18 11 375000 3 2003-07-18 12 60000 4 2003-07-18 8 1020000 5 2003-07-18 9 390000 I have been trying create a new data frame with the following day total_daily_volume I can accomplish this by bringing the data to excel and using a pivot table but I would like to be able to do in R. I have been searching for several hours but I can not figure out how to sum the the volume of each day. Thanks, Max
?aggregate day hour volume 1 2003-07-18 10 836700 2 2003-07-18 11 375000 3 2003-07-18 12 60000 4 2003-07-18 8 1020000 5 2003-07-18 9 390000> aggregate(x$volume, list(x$day), sum)Group.1 x 1 2003-07-18 2681700 On Wed, Oct 1, 2008 at 9:58 AM, Max Rausch <maxrausch at gmail.com> wrote:> I have a data frame with the following information > > day hour volume > 1 2003-07-18 10 836700 > 2 2003-07-18 11 375000 > 3 2003-07-18 12 60000 > 4 2003-07-18 8 1020000 > 5 2003-07-18 9 390000 > > I have been trying create a new data frame with the following > > day total_daily_volume > > I can accomplish this by bringing the data to excel and using a pivot table > but I would like to be able to do in R. I have been searching for several > hours but I can not figure out how to sum the the volume of each day. > > Thanks, > > Max > > ______________________________________________ > 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. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve?
tapply(data$volume, data$day, sum) would do I think. But I haven't tried, was to lazy to type in some data, I'm sorry. Gabor On Wed, Oct 1, 2008 at 3:58 PM, Max Rausch <maxrausch at gmail.com> wrote:> I have a data frame with the following information > > day hour volume > 1 2003-07-18 10 836700 > 2 2003-07-18 11 375000 > 3 2003-07-18 12 60000 > 4 2003-07-18 8 1020000 > 5 2003-07-18 9 390000 > > I have been trying create a new data frame with the following > > day total_daily_volume > > I can accomplish this by bringing the data to excel and using a pivot table > but I would like to be able to do in R. I have been searching for several > hours but I can not figure out how to sum the the volume of each day. > > Thanks, > > Max > > ______________________________________________ > 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. >-- Gabor Csardi <Gabor.Csardi at unil.ch> UNIL DGM
Two approaches ?aggregate or the reshape package aggregate(xx$volume, list(day=xx$day), sum) library(reshape) names(xx)[3] <- "value" cast(xx, day ~ ., sum) --- On Wed, 10/1/08, Max Rausch <maxrausch at gmail.com> wrote:> From: Max Rausch <maxrausch at gmail.com> > Subject: [R] "group by" functionality in R > To: r-help at r-project.org > Received: Wednesday, October 1, 2008, 9:58 AM > I have a data frame with the following information > > day hour volume > 1 2003-07-18 10 836700 > 2 2003-07-18 11 375000 > 3 2003-07-18 12 60000 > 4 2003-07-18 8 1020000 > 5 2003-07-18 9 390000 > > I have been trying create a new data frame with the > following > > day total_daily_volume > > I can accomplish this by bringing the data to excel and > using a pivot > table but I would like to be able to do in R. I have been > searching for > several hours but I can not figure out how to sum the the > volume of > each day. > > Thanks, > > Max > > ______________________________________________ > 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.
On Wed, Oct 1, 2008 at 8:58 AM, Max Rausch <maxrausch at gmail.com> wrote:> I have a data frame with the following information > > day hour volume > 1 2003-07-18 10 836700 > 2 2003-07-18 11 375000 > 3 2003-07-18 12 60000 > 4 2003-07-18 8 1020000 > 5 2003-07-18 9 390000 > > I have been trying create a new data frame with the following > > day total_daily_volume > > I can accomplish this by bringing the data to excel and using a pivot table > but I would like to be able to do in R. I have been searching for several > hours but I can not figure out how to sum the the volume of each day.You might want to have a look at the plyr (http://had.co.nz/plyr) and reshape (http://had.co.nz/reshape) packages which provide similar functionality to pivot tables in excel. Hadley -- http://had.co.nz/
Try this also; xtabs(volume ~ day, data = x) On Wed, Oct 1, 2008 at 10:58 AM, Max Rausch <maxrausch at gmail.com> wrote:> I have a data frame with the following information > > day hour volume > 1 2003-07-18 10 836700 > 2 2003-07-18 11 375000 > 3 2003-07-18 12 60000 > 4 2003-07-18 8 1020000 > 5 2003-07-18 9 390000 > > I have been trying create a new data frame with the following > > day total_daily_volume > > I can accomplish this by bringing the data to excel and using a pivot table > but I would like to be able to do in R. I have been searching for several > hours but I can not figure out how to sum the the volume of each day. > > Thanks, > > Max > > ______________________________________________ > 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. >-- Henrique Dallazuanna Curitiba-Paran?-Brasil 25? 25' 40" S 49? 16' 22" O