[Rd] [R] Aggregate?
Gabor Grothendieck
ggrothendieck at gmail.com
Thu May 4 02:36:43 CEST 2006
I am moving this from r-help to r-devel.
The poster pointed out to me that my solution works in 2.2.1 but not
in 2.3.0 patched. Does anyone know what the problem is?
> # 2.3.0 patched -- gives error
> DF <- data.frame(A = gl(2,2), B = gl(2,2), C = 1:4) # test data
> out.by <- by(DF, DF$A, function(x) replace(x[1,], "C", sum(x$C)))
> do.call(rbind, out.by)
Error in data.frame(A = c("1", "2"), B = c("1", "2"), C = c("3", "7"), :
row names contain missing values
> R.version.string # Windows XP
[1] "Version 2.3.0 Patched (2006-04-28 r37936)"
> # 2.3.1 -- works ok
> DF <- data.frame(A = gl(2,2), B = gl(2,2), C = 1:4) # test data
> out.by <- by(DF, DF$A, function(x) replace(x[1,], "C", sum(x$C)))
> do.call(rbind, out.by)
A B C
1 1 1 3
2 2 2 7
> R.version.string # Windows XP
[1] "R version 2.2.1, 2005-12-20"
On 5/3/06, Gabor Grothendieck <ggrothendieck at gmail.com> wrote:
> 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-devel
mailing list