[R] summing and combining rows

R. Michael Weylandt michael.weylandt at gmail.com
Tue Aug 7 22:55:31 CEST 2012


On Tue, Aug 7, 2012 at 12:47 PM, Christopher R. Dolanc
<crdolanc at ucdavis.edu> wrote:
> 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

Which variable do you want to sum? You'll want to use something like

aggregate( XXX ~ YYY, data = dat.name, FUN = sum)

to sum variable XXX by classes YYY.

See ? aggregate for more details and worked examples.

Cheers,
Michael

>  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.
>



More information about the R-help mailing list