EVOLUTION-MANAGER
Edit File: groupingsets.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><title>R: Grouping Set aggregation for data tables</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <link rel="stylesheet" type="text/css" href="R.css" /> </head><body> <table width="100%" summary="page for groupingsets {data.table}"><tr><td>groupingsets {data.table}</td><td style="text-align: right;">R Documentation</td></tr></table> <h2> Grouping Set aggregation for data tables </h2> <h3>Description</h3> <p>Calculate aggregates at various levels of groupings producing multiple (sub-)totals. Reflects SQLs <em>GROUPING SETS</em> operations. </p> <h3>Usage</h3> <pre> rollup(x, ...) ## S3 method for class 'data.table' rollup(x, j, by, .SDcols, id = FALSE, ...) cube(x, ...) ## S3 method for class 'data.table' cube(x, j, by, .SDcols, id = FALSE, ...) groupingsets(x, ...) ## S3 method for class 'data.table' groupingsets(x, j, by, sets, .SDcols, id = FALSE, jj, ...) </pre> <h3>Arguments</h3> <table summary="R argblock"> <tr valign="top"><td><code>x</code></td> <td> <p><code>data.table</code>.</p> </td></tr> <tr valign="top"><td><code>...</code></td> <td> <p>argument passed to custom user methods. Ignored for <code>data.table</code> methods.</p> </td></tr> <tr valign="top"><td><code>j</code></td> <td> <p>expression passed to data.table <code>j</code>.</p> </td></tr> <tr valign="top"><td><code>by</code></td> <td> <p>character column names by which we are grouping.</p> </td></tr> <tr valign="top"><td><code>sets</code></td> <td> <p>list of character vector reflecting grouping sets, used in <code>groupingsets</code> for flexibility.</p> </td></tr> <tr valign="top"><td><code>.SDcols</code></td> <td> <p>columns to be used in <code>j</code> expression in <code>.SD</code> object.</p> </td></tr> <tr valign="top"><td><code>id</code></td> <td> <p>logical default <code>FALSE</code>. If <code>TRUE</code> it will add leading column with bit mask of grouping sets.</p> </td></tr> <tr valign="top"><td><code>jj</code></td> <td> <p>quoted version of <code>j</code> argument, for convenience. When provided function will ignore <code>j</code> argument.</p> </td></tr> </table> <h3>Details</h3> <p>All three functions <code>rollup, cube, groupingsets</code> are generic methods, <code>data.table</code> methods are provided. </p> <h3>Value</h3> <p>A data.table with various aggregates. </p> <h3>References</h3> <p><a href="https://www.postgresql.org/docs/9.5/static/queries-table-expressions.html#QUERIES-GROUPING-SETS">https://www.postgresql.org/docs/9.5/static/queries-table-expressions.html#QUERIES-GROUPING-SETS</a> <a href="https://www.postgresql.org/docs/9.5/static/functions-aggregate.html#FUNCTIONS-GROUPING-TABLE">https://www.postgresql.org/docs/9.5/static/functions-aggregate.html#FUNCTIONS-GROUPING-TABLE</a> </p> <h3>See Also</h3> <p><code><a href="data.table.html">data.table</a></code>, <code><a href="rbindlist.html">rbindlist</a></code> </p> <h3>Examples</h3> <pre> n = 24L set.seed(25) DT <- data.table( color = sample(c("green","yellow","red"), n, TRUE), year = as.Date(sample(paste0(2011:2015,"-01-01"), n, TRUE)), status = as.factor(sample(c("removed","active","inactive","archived"), n, TRUE)), amount = sample(1:5, n, TRUE), value = sample(c(3, 3.5, 2.5, 2), n, TRUE) ) # rollup rollup(DT, j = sum(value), by = c("color","year","status")) # default id=FALSE rollup(DT, j = sum(value), by = c("color","year","status"), id=TRUE) rollup(DT, j = lapply(.SD, sum), by = c("color","year","status"), id=TRUE, .SDcols="value") rollup(DT, j = c(list(count=.N), lapply(.SD, sum)), by = c("color","year","status"), id=TRUE) # cube cube(DT, j = sum(value), by = c("color","year","status"), id=TRUE) cube(DT, j = lapply(.SD, sum), by = c("color","year","status"), id=TRUE, .SDcols="value") cube(DT, j = c(list(count=.N), lapply(.SD, sum)), by = c("color","year","status"), id=TRUE) # groupingsets groupingsets(DT, j = c(list(count=.N), lapply(.SD, sum)), by = c("color","year","status"), sets = list("color", c("year","status"), character()), id=TRUE) </pre> <hr /><div style="text-align: center;">[Package <em>data.table</em> version 1.14.4 <a href="00Index.html">Index</a>]</div> </body></html>