clair.crossupton at googlemail.com
2009-Sep-09 10:51 UTC
[R] How to sum and group data by DATE in data frame
Dear all, Lets say I have a data frame as follows:> Date <- as.Date(c('2006-08-23', '2006-08-30', '2006-09-06', '2006-09-13', '2006-09-20')) > Income <- c(73.79, 72.46, 76.32, 72.43, 72.62) > data.frame(Date, Income)Date Income 1 2006-08-23 73.79 2 2006-08-30 72.46 3 2006-09-06 76.32 4 2006-09-13 72.43 5 2006-09-20 72.62>is there a way to group the data by month (summing the values in each month), i.e. Date Income 2006-08 146.25 2006-09 221.37 Thanks in advance, C.C.
Hi Clair, try to use this code aggregate(toto$Income,by=list((substr(toto$Date,1,7))),sum) Regards mohamed clair.crossupton at googlemail.com a ?crit :> Dear all, > > Lets say I have a data frame as follows: > > > >> Date <- as.Date(c('2006-08-23', '2006-08-30', '2006-09-06', '2006-09-13', '2006-09-20')) >> Income <- c(73.79, 72.46, 76.32, 72.43, 72.62) >> data.frame(Date, Income) >> > Date Income > 1 2006-08-23 73.79 > 2 2006-08-30 72.46 > 3 2006-09-06 76.32 > 4 2006-09-13 72.43 > 5 2006-09-20 72.62 > > > is there a way to group the data by month (summing the values in each > month), i.e. > > Date Income > 2006-08 146.25 > 2006-09 221.37 > > Thanks in advance, > C.C. > > ______________________________________________ > 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. > >-- Mohamed Lajnef INSERM Unit? 955. 40 rue de Mesly. 94000 Cr?teil. Courriel : Mohamed.lajnef at inserm.fr tel. : 01 49 81 31 31 (poste 18470) Sec : 01 49 81 32 90 fax : 01 49 81 30 99
Henrique Dallazuanna
2009-Sep-09 11:48 UTC
[R] How to sum and group data by DATE in data frame
Try this: rowsum(Income, format(Date, '%m-%Y')) or tapply(Income, format(Date, '%m-%Y'), sum) On Wed, Sep 9, 2009 at 7:51 AM, clair.crossupton@googlemail.com < clair.crossupton@googlemail.com> wrote:> Dear all, > > Lets say I have a data frame as follows: > > > > Date <- as.Date(c('2006-08-23', '2006-08-30', '2006-09-06', '2006-09-13', > '2006-09-20')) > > Income <- c(73.79, 72.46, 76.32, 72.43, 72.62) > > data.frame(Date, Income) > Date Income > 1 2006-08-23 73.79 > 2 2006-08-30 72.46 > 3 2006-09-06 76.32 > 4 2006-09-13 72.43 > 5 2006-09-20 72.62 > > > > is there a way to group the data by month (summing the values in each > month), i.e. > > Date Income > 2006-08 146.25 > 2006-09 221.37 > > Thanks in advance, > C.C. > > ______________________________________________ > R-help@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 [[alternative HTML version deleted]]
Reasonably Related Threads
- Function to find angle between coordinates?
- How to find moving averages within each subgroup of a data frame
- Using n+1 instances of R to utilise n processors on one machine - something like R with tabbed browsing?
- RCurl unable to download a particular web page -- what is so special about this web page?
- binary