James R. Milks
2007-Jul-02 03:15 UTC
[R] Extracting sums for individual factors in data frames
I have a data frame with two columns, one of which is a factor (Species) and the other is numeric (BA, which stands for basal area). Here's a sample: Species BA ACSA 55.7632696 FRAM 122.9933524 ACSA 67.54424205 ACSA 89.22123136 ACSA 82.46680716 ACSA 22.46238747 ACSA 19.94911335 ACSA 20.42035225 ACSA 19.00663555 ACSA 21.67698931 ACSA 57.80530483 ACSA 30.31636911 Dead 43.98229715 Dead 40.21238597 Dead 16.49336143 Dead 40.21238597 Dead 16.49336143 ACSA 78.53981634 VIPR 3.926990817 AEGL 11.78097245 AEGL 0 AEGL 0 ACSA 0 ACSA 0 ACSA 0 VIPR 0 I would like to calculate relative basal area for each species in this plot. For that, I need to divide the total basal area per species by the total basal area in the plot. Getting the total basal area in the plot is easy. However, I'm mystified on how to get the total basal area per species. Is there a way to extract and/or sum the total basal area per species? Thank you in advance. Jim Milks Graduate Student Environmental Sciences Ph.D. Program Wright State University 3640 Colonel Glenn Hwy Dayton, OH 45435 [[alternative HTML version deleted]]
Rolf Turner
2007-Jul-02 03:30 UTC
[R] Extracting sums for individual factors in data frames
?tapply ###################################################################### Attention:\ This e-mail message is privileged and confidenti...{{dropped}}
jim holtman
2007-Jul-02 03:34 UTC
[R] Extracting sums for individual factors in data frames
Does this do what you want?> x <- "Species BA+ ACSA 55.7632696 + FRAM 122.9933524 + ACSA 67.54424205 + ACSA 89.22123136 + ACSA 82.46680716 + ACSA 22.46238747 + ACSA 19.94911335 + ACSA 20.42035225 + ACSA 19.00663555 + ACSA 21.67698931 + ACSA 57.80530483 + ACSA 30.31636911 + Dead 43.98229715 + Dead 40.21238597 + Dead 16.49336143 + Dead 40.21238597 + Dead 16.49336143 + ACSA 78.53981634 + VIPR 3.926990817 + AEGL 11.78097245 + AEGL 0 + AEGL 0 + ACSA 0 + ACSA 0 + ACSA 0 + VIPR 0"> x <- read.table(textConnection(x), header=TRUE) > # compute area for each species > y <- tapply(x$BA, x$Species, sum) > # get ratio > y/sum(x$BA)ACSA AEGL Dead FRAM VIPR 0.656210104 0.013678643 0.182746672 0.142805034 0.004559548> >On 7/1/07, James R. Milks <james.milks@wright.edu> wrote:> > I have a data frame with two columns, one of which is a factor > (Species) and the other is numeric (BA, which stands for basal > area). Here's a sample: > > > Species BA > ACSA 55.7632696 > FRAM 122.9933524 > ACSA 67.54424205 > ACSA 89.22123136 > ACSA 82.46680716 > ACSA 22.46238747 > ACSA 19.94911335 > ACSA 20.42035225 > ACSA 19.00663555 > ACSA 21.67698931 > ACSA 57.80530483 > ACSA 30.31636911 > Dead 43.98229715 > Dead 40.21238597 > Dead 16.49336143 > Dead 40.21238597 > Dead 16.49336143 > ACSA 78.53981634 > VIPR 3.926990817 > AEGL 11.78097245 > AEGL 0 > AEGL 0 > ACSA 0 > ACSA 0 > ACSA 0 > VIPR 0 > > I would like to calculate relative basal area for each species in > this plot. For that, I need to divide the total basal area per > species by the total basal area in the plot. Getting the total basal > area in the plot is easy. However, I'm mystified on how to get the > total basal area per species. Is there a way to extract and/or sum > the total basal area per species? > > Thank you in advance. > > Jim Milks > > Graduate Student > Environmental Sciences Ph.D. Program > Wright State University > 3640 Colonel Glenn Hwy > Dayton, OH 45435 > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@stat.math.ethz.ch 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. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem you are trying to solve? [[alternative HTML version deleted]]
Simon Blomberg
2007-Jul-02 04:24 UTC
[R] Extracting sums for individual factors in data frames
Does this do what you want?> with(dat, tapply(BA, Species, sum))ACSA AEGL Dead FRAM VIPR 565.172518 11.780972 157.393792 122.993352 3.926991 Cheers, Simon. On Sun, 2007-07-01 at 23:15 -0400, James R. Milks wrote:> I have a data frame with two columns, one of which is a factor > (Species) and the other is numeric (BA, which stands for basal > area). Here's a sample: > > > Species BA > ACSA 55.7632696 > FRAM 122.9933524 > ACSA 67.54424205 > ACSA 89.22123136 > ACSA 82.46680716 > ACSA 22.46238747 > ACSA 19.94911335 > ACSA 20.42035225 > ACSA 19.00663555 > ACSA 21.67698931 > ACSA 57.80530483 > ACSA 30.31636911 > Dead 43.98229715 > Dead 40.21238597 > Dead 16.49336143 > Dead 40.21238597 > Dead 16.49336143 > ACSA 78.53981634 > VIPR 3.926990817 > AEGL 11.78097245 > AEGL 0 > AEGL 0 > ACSA 0 > ACSA 0 > ACSA 0 > VIPR 0 > > I would like to calculate relative basal area for each species in > this plot. For that, I need to divide the total basal area per > species by the total basal area in the plot. Getting the total basal > area in the plot is easy. However, I'm mystified on how to get the > total basal area per species. Is there a way to extract and/or sum > the total basal area per species? > > Thank you in advance. > > Jim Milks > > Graduate Student > Environmental Sciences Ph.D. Program > Wright State University > 3640 Colonel Glenn Hwy > Dayton, OH 45435 > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at stat.math.ethz.ch 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.-- Simon Blomberg, BSc (Hons), PhD, MAppStat. Lecturer and Consultant Statistician Faculty of Biological and Chemical Sciences The University of Queensland St. Lucia Queensland 4072 Australia Room 320, Goddard Building (8) T: +61 7 3365 2506 email: S.Blomberg1_at_uq.edu.au The combination of some data and an aching desire for an answer does not ensure that a reasonable answer can be extracted from a given body of data. - John Tukey.
Norm.Good at csiro.au
2007-Jul-10 00:16 UTC
[R] Extracting sums for individual factors in data frames
Try this I used today aggregate(data, by=list(factor.name=data$FACTOR.NAME), sum) Norm Good CMIS/e-Health Research Centre A joint venture between CSIRO and the Queensland Government Lvl 20, 300 Adelaide Street BRISBANE QLD 4000 PO Box 10842 Adelaide Street BRISBANE QLD 4000 Ph: 07 3024 1640 Fx: 07 3024 1690 Em: norm.good@csiro.au Web: http://e-hrc.net/ [[alternative HTML version deleted]]