[R] Summing identical IDs

Dimitris Rizopoulos d.rizopoulos at erasmusmc.nl
Thu Oct 29 20:23:17 CET 2009


one option is the following:

Acres <- c(100,101,100,130,156,.5,293,300,.09)
Bldgid <- c(1,2,3,4,5,5,6,7,7)
DF <- data.frame(Acres, Bldgid)

aggregate(DF, list(Bldgid), sum)


I hope it helps.

Best,
Dimitris


PDXRugger wrote:
> Hello All, 
>    I would like to select records with identical IDs then sum an attribute
> then and return them to the data frame as a single record.  Please consider
> 
> 
> Acres<-c(100,101,100,130,156,.5,293,300,.09)
> Bldgid<-c(1,2,3,4,5,5,6,7,7)
> 
> DF=cbind(Acres,Bldgid)
> DF<-as.data.frame(DF)
> 
> So that:
> 
>   Acres Bldgid
> 1 100.00      1
> 2 101.00      2
> 3 100.00      3
> 4 130.00      4
> 5 156.00      5
> 6   0.50      5
> 7 293.00      6
> 8 300.00      7
> 9   0.09      7
> 
> Becomes
> 
>   Acres Bldgid
> 1 100.00      1
> 2 101.00      2
> 3 100.00      3
> 4 130.00      4
> 5 156.50      5
> 7 293.00      6
> 8 300.09      7
> 
> dup<-unique(DF$Bldgid[duplicated(Bldgid)])
> dupbuild<-DF[DF$Bldgid %in% dup,]
> dupbuild..dupareasum<-sum(dupbuild$Acres[duplicated(dupbuild$Bldgid)])
> 
> This sums the unique Ids of the duplicated records, not whati want.  Thanks
> ahead of time
> 
> JR
> 
> 

-- 
Dimitris Rizopoulos
Assistant Professor
Department of Biostatistics
Erasmus University Medical Center

Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands
Tel: +31/(0)10/7043478
Fax: +31/(0)10/7043014




More information about the R-help mailing list