Skip to contents

Assign a group to each row of a data.table. If by is specified, then each unique combination of the columns in by will be assigned a unique group. If by is not specified, then all rows will be assigned to the same group.

Usage

enw_assign_group(obs, by = NULL, copy = TRUE)

Arguments

obs

A data.table or data.frame without a .group column.

by

A character vector of column names to group by. Defaults to an empty vector.

copy

A logical; make a copy (default) of obs or modify it in place?

Value

A data.table with a .group column added ordered by .group and the existing key of obs.

Examples

obs <- data.frame(x = 1:3, y = 1:3)
enw_assign_group(obs)
#> Key: <.group>
#>        x     y .group
#>    <int> <int>  <num>
#> 1:     1     1      1
#> 2:     2     2      1
#> 3:     3     3      1
enw_assign_group(obs, by = "x")
#> Key: <.group>
#>        x     y .group
#>    <int> <int>  <int>
#> 1:     1     1      1
#> 2:     2     2      2
#> 3:     3     3      3