collapse: Collapse a data.table by Group

Description Usage Arguments Details Examples

Description

For each data.table subset (as subset by the by argument), return the rows at indices i.

Usage

1
collapse(DT, i = 1, by)

Arguments

DT

A data.table.

i

Indices at which to subset each sub-data.table.

by

Variable(s) to subset over, as a list of symbols.

Details

The code wraps to the impossible to remember but incredibly powerful and fast DT[ DT[, .I[i], by=by]$V1 ].

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
DT <- data.table(
  x=rep(letters, each=1E5),
  y=rnorm(26*1E5),
  z=rep(LETTERS, each=1E5),
  a=rep(LETTERS, each=1E5),
  b=rep(LETTERS, each=1E5),
  c=rep(LETTERS, each=1E5),
  d=rep(LETTERS, each=1E5),
  e=rep(LETTERS, each=1E5)
)

DT[, mean_y := mean(y), by=x]
collapse(DT, 1, by=list(x))

kevinushey/data.table.extras documentation built on May 20, 2019, 9:09 a.m.