Skip to content
Prev 302642 / 398503 Next

summing and combining rows

Hi,
Not sure why you mentioned "+" doesn't work.
dat1<-read.table(text="
Plot??????? Elevation??????? Area??????? SizeClass??? Stems
12??????????? 1200??????????? 132.4??????? Class3??????????? 0
12??????????? 1200??????????? 132.4??????? Class4??????????? 1
17??????????? 2320??????????? 209.1??????? Class3??????????? 3
17??????????? 2320??????????? 209.1??????? Class4??????????? 5
",sep="",header=TRUE,stringsAsFactors=FALSE)

dat2<-aggregate(Stems~Plot+Elevation+Area, data=dat1,sum)
?dat3<-data.frame(dat2,SizeClass=rep("Class34",2))
?dat3<-dat3[,c(1:3,5,4)]
?dat3
#? Plot Elevation? Area SizeClass Stems
#1?? 12????? 1200 132.4?? Class34???? 1
#2?? 17????? 2320 209.1?? Class34???? 8

A.K.






----- Original Message -----
From: Christopher R. Dolanc <crdolanc at ucdavis.edu>
To: arun <smartpink111 at yahoo.com>
Cc: 
Sent: Tuesday, August 7, 2012 5:47 PM
Subject: Re: [R] summing and combining rows

Thanks for your response. The aggregate method mostly works for me, but 
I have numerous other columns that I'd like to keep in the result. So, 
if I have something like this:


Plot? ? ? ? Elevation? ? ? ? Area? ? ? ? SizeClass? ? Stems
12? ? ? ? ? ? 1200? ? ? ? ? ? 132.4? ? ? ? Class3? ? ? ? ? ? 0
12? ? ? ? ? ? 1200? ? ? ? ? ? 132.4? ? ? ? Class4? ? ? ? ? ? 1
17? ? ? ? ? ? 2320? ? ? ? ? ? 209.1? ? ? ? Class3? ? ? ? ? ? 3
17? ? ? ? ? ? 2320? ? ? ? ? ? 209.1? ? ? ? Class4? ? ? ? ? ? 5

How can I make it look like this?

Plot? ? ? ? Elevation? ? ? ? Area? ? ? ? SizeClass? ? Stems
12? ? ? ? ? ? 1200? ? ? ? ? ? 132.4? ? ? ? Class34? ? ? ?  1
17? ? ? ? ? ? 2320? ? ? ? ? ? 209.1? ? ? ? Class34? ? ? ?  8

I see something in ?aggregate about adding columns with a +, but this 
doesn't quite work for me.
On 8/7/2012 2:32 PM, arun wrote: