Thanks to visit codestin.com
Credit goes to github.com

Skip to content

grouping sets #1377

@jangorecki

Description

@jangorecki

Some keywords: GROUPING SETS, ROLLUP, CUBE, GROUPING
Some references: postgres, Oracle, SQL Server, groupings combined with arbitrary functions

Grouping sets and friends are useful to pre-calculate various aggregation levels, which is often desired. Api for that feature in data.table is not very friendly, see Aggregating sub totals and grand totals with data.table.

In case of rollup those are aggregations for provided by from top to bottom. See description from postgres man, and example code below.

ROLLUP ( e1, e2, e3, ... )

is equivalent to:

GROUPING SETS (
    ( e1, e2, e3, ... ),
    ...
    ( e1, e2 )
    ( e1 )
    ( )
)

I wonder if there could be cheap speed-up of that process? this is potentially heavy computing task. Would be great to have computation of grouping sets feature developed in C, so all the rollup/cube and other features could be built on top of grouping sets more easily in R still utilizing full speed.


Answers to update when closed:

library(plyr)
grp.cols <- c("vs", "am", "gear", "carb", "cyl")
plyr.r = do.call(
    rbind.fill,
    lapply(1:length(grp.cols), function(x) ddply(mtcars, grp.cols[1:x], summarize, agg=mean(mpg)))
)

library(data.table) # 1.9.7+
dt.r = rollup(as.data.table(mtcars), j = .(agg=mean(mpg)), by=grp.cols)
all.equal(
    as.data.table(plyr.r),
    dt.r[-.N], # exclude grand total, not present in BrodieG answer
    ignore.row.order = TRUE,
    ignore.col.order = TRUE
)
#[1] TRUE
# install.packages("data.table", type = "source", repos = "https://Rdatatable.github.io/data.table")
library(data.table)
set.seed(1)
DT = data.table(
    group=sample(letters[1:2],100,replace=TRUE), 
    year=sample(2010:2012,100,replace=TRUE),
    v=runif(100))

cube(DT, mean(v), by=c("group","year"))
#    group year        V1
#1:     a 2011 0.4176346
#2:     b 2010 0.5231845
#3:     b 2012 0.4306871
#4:     b 2011 0.4997119
#5:     a 2012 0.4227796
#6:     a 2010 0.2926945
#7:    NA 2011 0.4463616
#8:    NA 2010 0.4278093
#9:    NA 2012 0.4271160
#10:     a   NA 0.3901875
#11:     b   NA 0.4835788
#12:    NA   NA 0.4350153
cube(DT, mean(v), by=c("group","year"), id=TRUE)
#    grouping group year        V1
#1:        0     a 2011 0.4176346
#2:        0     b 2010 0.5231845
#3:        0     b 2012 0.4306871
#4:        0     b 2011 0.4997119
#5:        0     a 2012 0.4227796
#6:        0     a 2010 0.2926945
#7:        2    NA 2011 0.4463616
#8:        2    NA 2010 0.4278093
#9:        2    NA 2012 0.4271160
#10:        1     a   NA 0.3901875
#11:        1     b   NA 0.4835788
#12:        3    NA   NA 0.4350153

# install.packages("data.table", type = "source", repos = "https://Rdatatable.github.io/data.table")

Some other questions can get new answers also:

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions