[R] Summing Data in R

jim holtman jholtman at gmail.com
Tue Dec 27 18:38:59 CET 2011


Is this what you are after?  Send your sample data using 'dput/dump'
next time -- easier to read in:

> x <- read.table(text = "License  Species  Year      HD     Quota    L.R.QTA    L.R.QTA    Success Surplus
+ Permit    Elk            1999    101     50           87
    90 151           10
+ Permit    Deer        1999    101     50           20
 10 151           8
+ Permit    Elk            1999    101     50           87
    90 20             9
+ Permit    Elk            1999    101     50           60
    20 151           10
+ A9           Elk            1999    101     40           87 30
     241           10
+ A9           Elk            1999    101     20           50 50
     154           8
+ A9           Deer        2000    101     50           87
    70 161           10
+ A9           Deer        2000    101     30           80
    70 181           16
+ DeerB    Elk            1999    201     50           87
   70 161           19
+ DeerB    Elk            1999    201     60          87
   90 151           18"
+     , header = TRUE
+     , as.is = TRUE
+     )
>
> aggregate(x[, c("Quota", "L.R.QTA", "L.R.QTA.1", "Success", "Surplus")]
+         , list(x$Year, x$HD)
+         , FUN = sum)
  Group.1 Group.2 Quota L.R.QTA L.R.QTA.1 Success Surplus
1    1999     101   260     391       290     868      55
2    2000     101    80     167       140     342      26
3    1999     201   110     174       160     312      37
>

On Tue, Dec 27, 2011 at 12:17 PM, steven.hazen
<steven.hazen at msu.montana.edu> wrote:
> Currently I have a data set looking like:
> License  Species  Year      HD     Quota    L.R.QTA    L.R.QTA    Success
> Surplus
> Permit    Elk            1999    101     50           87                 90
> 151           10
> Permit    Deer        1999    101     50           20                 10
> 151           8
> Permit    Elk            1999    101     50           87                 90
> 20             9
> Permit    Elk            1999    101     50           60                 20
> 151           10
> A9           Elk            1999    101     40           87
> 30            241           10
> A9           Elk            1999    101     20           50
> 50            154           8
> A9           Deer        2000    101     50           87                 70
> 161           10
> A9           Deer        2000    101     30           80                 70
> 181           16
> DeerB    Elk            1999    201     50           87                 70
> 161           19
> DeerB    Elk            1999    201     60          87                  90
> 151           18
>
> This dataset goes from 1999-2010 and I'm trying to sum all variables for by
> hunting district and year. Therefore instead of having individual stats by
> each HD I want to sum the stats that correspond to say year 1999 and HD 101.
> Does anyone know an easy/quick way to do this? I'm a new user of R and Any
> help would be much appreciated.
>
> Steve
>
> --
> View this message in context: http://r.789695.n4.nabble.com/Summing-Data-in-R-tp4237650p4237650.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> 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.



-- 
Jim Holtman
Data Munger Guru

What is the problem that you are trying to solve?
Tell me what you want to do, not how you want to do it.



More information about the R-help mailing list