[R] aggravating aggregate

William Dunlap wdunlap at tibco.com
Wed Aug 5 23:49:46 CEST 2015


# your data, from dump("xxx", file=stdout())
xxx <-
structure(list(Registered = c(1327, 2129, 10, 433, 5, 166, 1784,
17, 787), GA_Total = c(127, 150, 0, 32, 1, 76, 153, 7, 31), PCT = c("1120",
"1121", "1121", "1122", "1124", "1125", "1125", "1125", "1126"
)), .Names = c("Registered", "GA_Total", "PCT"), class = "data.frame",
row.names = c(NA,
-9L))
axxx <- aggregate(. ~ PCT, data=xxx, FUN=sum)
axxx
#   PCT Registered GA_Total
#1 1120       1327      127
#2 1121       2139      150
#3 1122        433       32
#4 1124          5        1
#5 1125       1967      236
#6 1126        787       31
# or
aggregate(xxx[, c("Registered", "GA_Total")], by=xxx[,"PCT",drop=FALSE],
sum)
#   PCT Registered GA_Total
#1 1120       1327      127
#2 1121       2139      150
#3 1122        433       32
#4 1124          5        1
#5 1125       1967      236
#6 1126        787       31

Use subscripting to rearrange the columns: axxx2 <- axxx[, c(2,3,1)].

Many people find the functions in the dplyr package easier to use.  E.g.,
library(dplyr)
group_by(xxx, PCT) %>% summarize(Registered=sum(Registered),
GA_Total=sum(GA_Total))
#Source: local data frame [6 x 3]
#
#   PCT Registered GA_Total
#1 1120       1327      127
#2 1121       2139      150
#3 1122        433       32
#4 1124          5        1
#5 1125       1967      236
#6 1126        787       31



Bill Dunlap
TIBCO Software
wdunlap tibco.com

On Wed, Aug 5, 2015 at 10:45 AM, Jim Burke <javajimburke at gmail.com> wrote:

> Greetings R mavens (and that's YOU). Given the following how on earth do I
> aggregate and sum duplicate PCT rows into one combined row per PCT?
>
> Data types
> > str(xxx)
> 'data.frame':   9 obs. of  3 variables:
>  $ Registered: int  1327 2129 10 433 5 166 1784 17 787 700
>  $ GA_Total  : int  127 150 0 32 1 76 153 7 31 23
>  $ PCT       : chr  "1120" "1121" "1121" "1122" ...
>
> Data desired to be summarized
> Registered GA_Total  PCT
> 1327       127      1120
> 2129       150      1121
>   10         0      1121
>  433        32      1122
>    5         1      1124
>  166        76      1125
> 1784       153      1125
>   17         7      1125
>  787        31      1126
>
> Desired summary
> Registered GA_Total  PCT
> 1327       127      1120
> 2139       150      1121
>  433        32      1122
>    5         1      1124
> 1967       236      1125
>  787        31      1126
>
> When answered this please make the reply suitable as a tutorial for many
> other puzzled "R" "aggregate" people. Scarce useful examples on the
> web. "Aggregate"
> is a basic for statistical programming. Yet even ISBN 978-1461471370 An
> Introduction to Statistical Learning with "R" does not appear to delve into
> "aggerate". Basic simple and useful fully explained answers. If you have to
> use "apply" then explain why.
>
> Thanks
> Jim Burke
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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.
>

	[[alternative HTML version deleted]]



More information about the R-help mailing list