EVOLUTION-MANAGER
Edit File: rleid.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: Generate run-length type group id</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 rleid {data.table}"><tr><td>rleid {data.table}</td><td style="text-align: right;">R Documentation</td></tr></table> <h2>Generate run-length type group id</h2> <h3>Description</h3> <p>A convenience function for generating a <em>run-length</em> type <em>id</em> column to be used in grouping operations. It accepts atomic vectors, lists, data.frames or data.tables as input. </p> <h3>Usage</h3> <pre> rleid(..., prefix=NULL) rleidv(x, cols=seq_along(x), prefix=NULL) </pre> <h3>Arguments</h3> <table summary="R argblock"> <tr valign="top"><td><code>x</code></td> <td> <p> A vector, list, data.frame or data.table. </p> </td></tr> <tr valign="top"><td><code>...</code></td> <td> <p> A sequence of numeric, integer64, character or logical vectors, all of same length. For interactive use.</p> </td></tr> <tr valign="top"><td><code>cols</code></td> <td> <p> Only meaningful for lists, data.frames or data.tables. A character vector of column names (or numbers) of x. </p> </td></tr> <tr valign="top"><td><code>prefix</code></td> <td> <p> Either <code>NULL</code> (default) or a character vector of length=1 which is prefixed to the row ids, returning a character vector (instead of an integer vector).</p> </td></tr> </table> <h3>Details</h3> <p>At times aggregation (or grouping) operations need to be performed where consecutive runs of identical values should belong to the same group (See <code><a href="../../base/html/rle.html">rle</a></code>). The use for such a function has come up repeatedly on StackOverflow, see the <code>See Also</code> section. This function allows to generate "run-length" groups directly. </p> <p><code>rleid</code> is designed for interactive use and accepts a sequence of vectors as arguments. For programming, <code>rleidv</code> might be more useful. </p> <h3>Value</h3> <p>When <code>prefix = NULL</code>, an integer vector with same length as <code>NROW(x)</code>, else a character vector with the value in <code>prefix</code> prefixed to the ids obtained. </p> <h3>See Also</h3> <p><code><a href="data.table.html">data.table</a></code>, <code><a href="rowid.html">rowid</a></code>, <a href="https://stackoverflow.com/q/21421047/559784">https://stackoverflow.com/q/21421047/559784</a> </p> <h3>Examples</h3> <pre> DT = data.table(grp=rep(c("A", "B", "C", "A", "B"), c(2,2,3,1,2)), value=1:10) rleid(DT$grp) # get run-length ids rleidv(DT, "grp") # same as above rleid(DT$grp, prefix="grp") # prefix with 'grp' # get sum of value over run-length groups DT[, sum(value), by=.(grp, rleid(grp))] DT[, sum(value), by=.(grp, rleid(grp, prefix="grp"))] </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>