[R] summing and combining rows

arun smartpink111 at yahoo.com
Wed Aug 8 00:00:23 CEST 2012



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:
> Hi,
>
> Try this:
> dat1<-read.table(text="
> Plot    SizeClass    Stems
> 12      Class3            1
> 12      Class4            3
> 17      Class3            5
> 17      Class4            2
> ",sep="",header=TRUE, stringsAsFactors=FALSE)
>
>
>
> ddply(dat1,.(Plot), summarize, sum(Stems))
>
> #or
>
>
> dat2<-aggregate(Stems~Plot,data=dat1,sum)
>   dat3<-data.frame(dat2,SizeClass=rep("Class34",2))
>   dat3
> #  Plot Stems SizeClass
> #1   12     4   Class34
> #2   17     7   Class34
>
>
> A.K.
>
> ----- Original Message -----
> From: Christopher R. Dolanc <crdolanc at ucdavis.edu>
> To: r-help at r-project.org
> Cc:
> Sent: Tuesday, August 7, 2012 1:47 PM
> Subject: [R] summing and combining rows
>
> Hello,
>
> I have a data set that needs to be combined so that rows are summed by a group based on a certain variable. I'm pretty sure rowsum() or rowsums() can do this but it's difficult for me to figure out how it will work for my data based on the examples I've read.
>
> My data are structured like this:
>
> Plot    SizeClass    Stems
> 12       Class3            1
> 12       Class4            3
> 17       Class3            5
> 17       Class4            2
>
> I simply want to sum the size classes by plot and create a new data frame with a size class called "Class34" or with the SizeClass variable removed. I actually do have other size classes that I want to leave alone, but combine 3 and 4, so if I could figure out how to do this by creating a new class, that would be preferable.
>
> I've also attached a more detailed sample of data.
>
> Thanks,
> Chris Dolanc
>
> -- Christopher R. Dolanc
> Post-doctoral Researcher
> University of Montana and UC-Davis
>
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
>

-- 
Christopher R. Dolanc
Post-doctoral Researcher
University of Montana and UC-Davis



More information about the R-help mailing list