data.table (version 1.9.6)

rleid: Generate run-length type group id

Description

A convenience function for generating a run-length type id column to be used in grouping operations. It accepts atomic vectors, lists, data.frames or data.tables as input.

Usage

rleid(...) rleidv(x, cols=seq_along(x))

Arguments

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.

Value

An integer vector with same length as NROW(x).

Details

At times aggregation (or grouping) operations need to be performed where consecutive runs of identical values should belong to the same group (See rle). The use for such a function has come up repeatedly on StackOverflow, see the See Also section. This function allows to generate "run-length" groups directly.

rleid is designed for interactive use and accepts a sequence of vectors as arguments. For programming, rleidv might be more useful.

See Also

data.table, http://stackoverflow.com/q/21421047/559784

Examples

Run this code
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
# get sum of value over run-length groups
DT[, sum(value), by=.(grp, rleid(grp))]

Run the code above in your browser using DataLab