I have ID's with data per year, but the years are repeated throughout the columns:
test1<-data.frame(
ID=c("P1","P1","P2","P2","P1","P2","P1","P1") ,
YEAR1 =c(10,30,50,40,50,45,12,8),
YEAR2=c(40,20,30,10,50,30,60,10),
YEAR3=c(300,200,170,150,150,120,90,100),
YEAR2= c(100,10,20,30,50,60,40,80))
and what I need to sum the data per year and get something like this:
result <- data.frame(
ID=c("P1","P2") ,
YEAR1 =c(110,135),
YEAR2=c(460,180),
YEAR3=c(840,440))
result
# ID YEAR1 YEAR2 YEAR3
#1 P1 110 460 840
#2 P2 135 180 440
I've tried it with aggregate
:
result <- aggregate(test1, by=list(test1$ID), FUN = sum)
But my problem is that does not sum the years that are equal, maybe is because years now is an attribute?