[R] Aggregate?

Gabor Grothendieck ggrothendieck at gmail.com
Wed May 3 22:27:44 CEST 2006


Suppose we want to sum C over levels of A and that B is constant
within levels of A.  Then:

DF <- data.frame(A = gl(2,2), B = gl(2,2), C = 1:4)  # test data
do.call("rbind", by(DF, DF$A, function(x) replace(x[1,], "C", sum(x$C))))



On 5/3/06, Guenther, Cameron <Cameron.Guenther at myfwc.com> wrote:
> Hello,
>
> I have a data set with a grouping variable (TRIPID) and  several other
> variables.  TRIPID is repeated in some areas and I would like to use a
> function like aggregate to sum the variable UNITS according to TRIPID.
> However I would also like to retain the other variables as they are in
> the data set with the new summed TRIPID.
>
> So what I have is something like this:
>
> YEAR    MONTH   DAY     CONTINUE        SPL     AREA    COUNTY  DEPTH
> DEPUNIT GEAR    GEAR2   TRAPS   SOAKTIME        UNITS   FACTOR  DISPOSIT
> NUMSETS TRIPST  TRIPID
> 1992            1           26              1         SP0073928       8
> 25         4           NA      1000000     NA          NA
> NA              161             1           NA              NA
> NA      0216    3399054     1992            1           26
> 1         SP0073928       8             25         4           NA
> 1000000     NA          NA              NA              8
> 1           NA              NA              NA      0216    3399054
> 1992            1           26              2         SP0004228       8
> 25         4           NA      1000000     NA          NA
> NA              161             1           NA              NA
> NA      0216    3399054      1992            1           26
> 2         SP0004228       8             25         4           NA
> 1000000     NA          NA              NA              8
> 1           NA              NA              NA      0216    3399054
> 1992            1           25              NA      SP0052652       8
> 25         4           NA      1000000     NA          NA
> NA              85              1           NA              NA
> NA      0216    3399057       1992            1           26
> NA      SP0037940       8             25         4           NA
> 1000000     NA          NA              NA              70
> 1           NA              NA              NA      0216    3399058
> 1992            1           27              NA      SP0072357       8
> 25         4           NA      1000000     NA          NA
> NA              15              1           NA              NA
> NA      0216    3399059
> 1992            1           27              NA      SP0072357       8
> 25         4           NA      1000000     NA          NA
> NA              20              1           NA              NA
> NA      0216    3399059     1992            1           27
> NA      SP0026324       8             25         4           NA
> 1000000     NA          NA              NA              8
> 1           NA              NA              NA      0216    3399060
> 1992            1           28              1         SP0072357       8
> 25         4           NA      1000000     NA          NA
> NA              200            1           NA              NA
> NA      0216    3399062
>
> And what I want is this:
>
> YEAR    MONTH   DAY     CONTINUE        SPL     AREA    COUNTY  DEPTH
> DEPUNIT GEAR    GEAR2   TRAPS   SOAKTIME        UNITS   FACTOR  DISPOSIT
> NUMSETS TRIPST  TRIPID
> 1992            1           26              1         SP0073928       8
> 25         4           NA      1000000     NA          NA
> NA              338            1           NA              NA
> NA      0216    3399054      1992            1           25
> NA      SP0052652       8             25         4           NA
> 1000000     NA          NA              NA              85
> 1           NA              NA              NA      0216    3399057
> 1992            1           26              NA      SP0037940       8
> 25         4           NA      1000000     NA          NA
> NA              70              1           NA              NA
> NA      0216    3399058
> 1992            1           27              NA      SP0072357       8
> 25         4           NA      1000000     NA          NA
> NA              35              1           NA              NA
> NA      0216    3399059
> 1992            1           27              NA      SP0026324       8
> 25         4           NA      1000000     NA          NA
> NA              8               1           NA              NA
> NA      0216    3399060
> 1992            1           28              1         SP0072357       8
> 25         4           NA      1000000     NA          NA
> NA              200            1           NA              NA
> NA      0216    3399062
>
>
> Does anyone know how to do this.  Data file is attached.
> Thanks in advance
>
> Cameron Guenther, Ph.D.
> Associate Research Scientist
> FWC/FWRI, Marine Fisheries Research
> 100 8th Avenue S.E.
> St. Petersburg, FL 33701
> (727)896-8626 Ext. 4305
> cameron.guenther at myfwc.com
>
>        [[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
>




More information about the R-help mailing list