[R] Aggregate data frame across columns

arun smartpink111 at yahoo.com
Wed Nov 7 19:44:09 CET 2012


HI,

Forgot about the 2nd part (by quarter):
set.seed(1)
dat2<-data.frame(matrix(sample(10:250,96,replace=TRUE),ncol=24))
names(dat2)<-c(paste0("2004",paste0("0",1:9),sep=""),paste0("2004",10:12),paste0("2005",paste0("0",1:9),sep=""),paste0("2005",10:12))
library(reshape)
dat3<-melt(dat2)
dat4<-within(dat3,{variable<-as.character(variable)})
library(xts)
dat5<-xts(dat4[,2],order.by=as.yearqtr(dat4[,1],"%Y%m"))
 aggregate(dat5[,1],by=list(index(dat5)), sum)
      
#2004 Q1 1534
#2004 Q2 1833
#2004 Q3 1445
#2004 Q4 1720
#2005 Q1 1444
#2005 Q2 1630
#2005 Q3 1780
#2005 Q4 1458

A.K.



----- Original Message -----
From: Keith Weintraub <kw1958 at gmail.com>
To: r-help at r-project.org
Cc: 
Sent: Wednesday, November 7, 2012 12:32 PM
Subject: [R] Aggregate data frame across columns

Folks,
  I have a data frame with columns 200401, 200402, ..., 201207, 201208.

These represent years/months. What would be the best way to sum these columns by year? What about by quarter?

Thanks for your time,
KW

--


    [[alternative HTML version deleted]]

______________________________________________
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.





More information about the R-help mailing list