Nest columns into a list-column

nest.(.df, ..., .by = NULL, .key = NULL, .names_sep = NULL)

Arguments

.df

A data.table or data.frame

...

Columns to be nested.

.by

Columns to nest by

.key

New column name if .by is used

.names_sep

If NULL, the names will be left alone. If a string, the names of the columns will be created by pasting together the inner column names and the outer column names.

Examples

df <- data.table(
  a = 1:3,
  b = 1:3,
  c = c("a", "a", "b"),
  d = c("a", "a", "b")
)

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

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

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