Most data operations are done on groups defined by variables. group_by will group the data.table by selected variables (setting them as keys), and arrange them in ascending order. group_exe could do computations by group, it receives an object returned by group_by.

group_by(.data, ...)

group_exe(.data, ...)

groups(x)

ungroup(x)

Arguments

.data

A data.table

...

For group_by:Variables to group by. For group_exe:Any data manipulation arguments that could be implemented on a data.table.

x

A data.table

Value

A data.table with keys

Details

For mutate and summarise, it is recommended to use the innate "by" parameter, which is faster. Once the data.table is grouped, the order is changed forever.

groups() could return a character vector of specified groups.

ungroup() would delete the keys in data.table.

Examples

a = as.data.table(iris)
a
#>      Sepal.Length Sepal.Width Petal.Length Petal.Width   Species
#>             <num>       <num>        <num>       <num>    <fctr>
#>   1:          5.1         3.5          1.4         0.2    setosa
#>   2:          4.9         3.0          1.4         0.2    setosa
#>   3:          4.7         3.2          1.3         0.2    setosa
#>   4:          4.6         3.1          1.5         0.2    setosa
#>   5:          5.0         3.6          1.4         0.2    setosa
#>  ---                                                            
#> 146:          6.7         3.0          5.2         2.3 virginica
#> 147:          6.3         2.5          5.0         1.9 virginica
#> 148:          6.5         3.0          5.2         2.0 virginica
#> 149:          6.2         3.4          5.4         2.3 virginica
#> 150:          5.9         3.0          5.1         1.8 virginica
a %>%
  group_by(Species) %>%
  group_exe(
    head(3)
  )
#> Key: <Species>
#>       Species Sepal.Length Sepal.Width Petal.Length Petal.Width
#>        <fctr>        <num>       <num>        <num>       <num>
#> 1:     setosa          5.1         3.5          1.4         0.2
#> 2:     setosa          4.9         3.0          1.4         0.2
#> 3:     setosa          4.7         3.2          1.3         0.2
#> 4: versicolor          7.0         3.2          4.7         1.4
#> 5: versicolor          6.4         3.2          4.5         1.5
#> 6: versicolor          6.9         3.1          4.9         1.5
#> 7:  virginica          6.3         3.3          6.0         2.5
#> 8:  virginica          5.8         2.7          5.1         1.9
#> 9:  virginica          7.1         3.0          5.9         2.1
groups(a)
#> [1] "Species"
ungroup(a)
groups(a)
#> NULL