EVOLUTION-MANAGER
Edit File: rowid.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 unique row ids within each group</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 rowid {data.table}"><tr><td>rowid {data.table}</td><td style="text-align: right;">R Documentation</td></tr></table> <h2> Generate unique row ids within each group</h2> <h3>Description</h3> <p>Convenience functions for generating a unique row ids within each group. It accepts atomic vectors, lists, data.frames or data.tables as input. </p> <p><code>rowid</code> is intended for interactive use, particularly along with the function <code>dcast</code> to generate unique ids directly in the formula. </p> <p><code>rowidv(DT, cols=c("x", "y"))</code> is equivalent to column <code>N</code> in the code <code>DT[, N := seq_len(.N), by=c("x", "y")]</code>. </p> <p>See examples for more. </p> <h3>Usage</h3> <pre> rowid(..., prefix=NULL) rowidv(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>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="dcast.data.table.html">dcast.data.table</a></code>, <code><a href="rleid.html">rleid</a></code> </p> <h3>Examples</h3> <pre> DT = data.table(x=c(20,10,10,30,30,20), y=c("a", "a", "a", "b", "b", "b"), z=1:6) rowid(DT$x) # 1,1,2,1,2,2 rowidv(DT, cols="x") # same as above rowid(DT$x, prefix="group") # prefixed with 'group' rowid(DT$x, DT$y) # 1,1,2,1,2,1 rowidv(DT, cols=c("x","y")) # same as above DT[, .(N=seq_len(.N)), by=.(x,y)]$N # same as above # convenient usage with dcast dcast(DT, x ~ rowid(x, prefix="group"), value.var="z") # x group1 group2 # 1: 10 2 3 # 2: 20 1 6 # 3: 30 4 5 </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>