Nest data.tables by group.

Note: nest_by() does not return a rowwise tidytable.

nest_by(.df, ..., .key = "data", .keep = FALSE)

Arguments

.df

A data.frame or data.table

...

Columns to group by. If empty nests the entire data.table. tidyselect compatible.

.key

Name of the new column created by nesting.

.keep

Should the grouping columns be kept in the list column.

Examples

df <- data.table(
  a = 1:5,
  b = 6:10,
  c = c(rep("a", 3), rep("b", 2)),
  d = c(rep("a", 3), rep("b", 2))
)

df %>%
  nest_by()
#> # A tidytable: 1 × 1
#>   data               
#>   <list>             
#> 1 <tidytable [5 × 4]>

df %>%
  nest_by(c, d)
#> # A tidytable: 2 × 3
#>   c     d     data               
#>   <chr> <chr> <list>             
#> 1 a     a     <tidytable [3 × 2]>
#> 2 b     b     <tidytable [2 × 2]>

df %>%
  nest_by(where(is.character))
#> # A tidytable: 2 × 3
#>   c     d     data               
#>   <chr> <chr> <list>             
#> 1 a     a     <tidytable [3 × 2]>
#> 2 b     b     <tidytable [2 × 2]>

df %>%
  nest_by(c, d, .keep = TRUE)
#> # A tidytable: 2 × 3
#>   c     d     data               
#>   <chr> <chr> <list>             
#> 1 a     a     <tidytable [3 × 4]>
#> 2 b     b     <tidytable [2 × 4]>