rowid.Rd
Convenience functions for generating a unique row ids within each group. It accepts atomic vectors, lists, data.frames or data.tables as input.
rowid
is intended for interactive use, particularly along with the function dcast
to generate unique ids directly in the formula.
rowidv(DT, cols=c("x", "y"))
is equivalent to column N
in the code DT[, N := seq_len(.N), by=c("x", "y")]
.
See examples for more.
rowid(..., prefix=NULL) rowidv(x, cols=seq_along(x), prefix=NULL)
x | A vector, list, data.frame or data.table. |
---|---|
... | A sequence of numeric, integer64, character or logical vectors, all of same length. For interactive use. |
cols | Only meaningful for lists, data.frames or data.tables. A character vector of column names (or numbers) of x. |
prefix | Either |
When prefix = NULL
, an integer vector with same length as NROW(x)
, else a character vector with the value in prefix
prefixed to the ids obtained.
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#> [1] 1 1 2 1 2 2rowidv(DT, cols="x") # same as above#> [1] 1 1 2 1 2 2rowid(DT$x, prefix="group") # prefixed with 'group'#> [1] "group1" "group1" "group2" "group1" "group2" "group2"rowid(DT$x, DT$y) # 1,1,2,1,2,1#> [1] 1 1 2 1 2 1#> [1] 1 1 2 1 2 1#> [1] 1 1 2 1 2 1#> x group1 group2 #> 1: 10 2 3 #> 2: 20 1 6 #> 3: 30 4 5# x group1 group2 # 1: 10 2 3 # 2: 20 1 6 # 3: 30 4 5