[R] Extracting sums for individual factors in data frames

Simon Blomberg s.blomberg1 at uq.edu.au
Mon Jul 2 06:24:08 CEST 2007


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.



More information about the R-help mailing list