MarcoD
MarcoD

Reputation: 137

Sum column values that match year in another column in R

I have the following dataframe

 y<-data.frame(c(2007,2008,2009,2009,2010,2010),c(10,13,10,11,9,10),c(5,6,5,7,4,7))
 colnames(y)<-c("year","a","b")

I want to have a final data.frame that adds together within the same year the values in "y$a" in the new "a" column and the values in "y$b" in the new "b" column so that it looks like this"

 year   a   b
 2007   10  5
 2008   13  6
 2009   21  12
 2010   19  11

The following loop has done it for me,

 years<- as.numeric(levels(factor(y$year)))
 add.a<- numeric(length(y[,1]))
 add.b<- numeric(length(y[,1]))
 for(i in years){
   ind<- which(y$year==i)
   add.a[ind]<- sum(as.numeric(as.character(y[ind,"a"])))
   add.b[ind]<- sum(as.numeric(as.character(y[ind,"b"])))
 }
 y.final<-data.frame(y$year,add.a,add.b)
 colnames(y.final)<-c("year","a","b")
 y.final<-subset(y.final,!duplicated(y.final$year))

but I just think there must be a faster command. Any ideas? Kindest regards, Marco

Upvotes: 1

Views: 3910

Answers (1)

talat
talat

Reputation: 70266

The aggregate function is a good choice for this sort of operation, type ?aggregate for more information about it.

aggregate(cbind(a,b) ~ year, data = y, sum)
#  year  a  b
#1 2007 10  5
#2 2008 13  6
#3 2009 21 12
#4 2010 19 11

Upvotes: 2

Related Questions