Skip to content

Summing Data in R

6 messages · steven.hazen, David Winsemius, William Dunlap +1 more

#
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.
#
Is this what you are after?  Send your sample data using 'dput/dump'
next time -- easier to read in:
+ 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
+     )
+         , 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:

  
    
#
Yes that is what I'm trying to do, however when I enter the code 

aggregate(dstats[,c('QUOTA','L.R.QTA','L.R.A.1ST','L.R.S.1ST','L.N.QTA','L.N.A.1ST','L.N.S.1ST','R.R.QTA','R.R.A.1ST','R.R.A.2ND','R.R.A.3RD','R.R.S.1ST','R.R.S.2ND','R.R.S.3R','R.N.QTA','R.N.A.1ST','R.N.A.2ND','R.N.A.3RD','R.N.S.1ST',R.N.S.2ND','R.N.S.3RD','T.SUCCESS','T.SURPLUS')],list(dstats$Year,dstats$HD),FUN=sum)

I'm getting an error message saying:

 Error: unexpected string constant in
"aggregate(dstats[,c('QUOTA','L.R.QTA','L.R.A.1ST','L.R.S.1ST','L.N.QTA','L.N.A.1ST','L.N.S.1ST','R.R.QTA','R.R.A.1ST','R.R.A.2ND','R.R.A.3RD','R.R.S.1ST','R.R.S.2ND','R.R.S.3R','R.N.QTA','R.N."
Any suggestions on how to fix this?

Thanks,
Steve

--
View this message in context: http://r.789695.n4.nabble.com/Summing-Data-in-R-tp4237650p4237796.html
Sent from the R help mailing list archive at Nabble.com.
#
On Dec 27, 2011, at 1:01 PM, steven.hazen wrote:

            
Yes. Don't mix single and double quotes.
#
Your command includes (at about character position 240):
  'R.N.S.1ST',R.N.S.2ND'
Note the missing quote after the comma.

When R's error message starts with
  Error:
instead of
  Error in <command>:
it usually means it could not parse the text that you typed
to make a command out of it.  (Perhaps it should say 'Error
while parsing input text'.)

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com
#
If the columns are contiguous, then try the following: (you will have
to determine the range to use)

aggregate(dstats[,c(3:25)],list(dstats$Year,dstats$HD),FUN=sum)
On Tue, Dec 27, 2011 at 2:22 PM, William Dunlap <wdunlap at tibco.com> wrote: