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"
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
try this:
> x <- as.data.frame(x)
> x
C1 C2 C3 F
R1 1 2 2 0.3
R2 2 2 2 0.5
R3 1 2 1 0.2
> do.call('rbind',by(x, list(x$C1, x$C2), function(z){z$F <- sum(z$F);
z[1,]}))
C1 C2 C3 F
R1 1 2 2 0.5
R2 2 2 2 0.5
>
On 3/23/07, Yuan, Qiaoping (NIH/NIAAA) [E] <[EMAIL PROTECTED]>
L PROTECTED]>
To: "Yuan, Qiaoping (NIH/NIAAA) [E]" <[EMAIL PROTECTED]>
Copies to: r-help@stat.math.ethz.ch
Subject: Re: [R] subtotal for same row data
> try this:
>
> > x <- as.data.frame(x)
> > x
>C1 C2 C