[R] subtotal for same row data

Yuan, Qiaoping (NIH/NIAAA) [E] qyuan at mail.nih.gov
Fri Mar 23 17:01:20 CET 2007


Hi, There,

I would like to subtotal the number in a specified column for all rows having the same data for specified columns. The following is the simple example:


> x=matrix(c(1,2,2,0.3,2,2,2,0.5,1,2,1,0.2),3,4,byrow=T)
> rownames(x)=c("R1","R2","R3")
> colnames(x)=c("C1","C2","C3","F")
> x
   C1 C2 C3   F
R1  1  2  2 0.3
R2  2  2  2 0.5
R3  1  2  1 0.2

I would like to get the subtotal in column "F" based on same row data in column "C1" and "C2". The result should be like

C1   C2     SumF
1    2    0.5          # This is 0.3 + 0.2 from R1 and R3
2    2    0.5

Is there a simple way to do this? Any help will be greatly appreciated.

Qiaoping Yuan



More information about the R-help mailing list