4

Before .key was deprecated I did this:

library(tidyverse)

mtcars %>% group_by(cyl) %>% nest(.key = "my_name")

The help of nest() points out that now this is performed using tidy select, but I don't know how.

Artem Sokolov
  • 13,196
  • 4
  • 43
  • 74
danilinares
  • 1,172
  • 1
  • 9
  • 28

3 Answers3

3

You can use the new nest_by function in dplyr 1.0.0 which works similar to what you had previously with nest.

library(dplyr)
mtcars %>% group_by(cyl) %>% nest_by(.key = "my_name")

#   cyl             my_name
#  <dbl> <list<tbl_df[,10]>>
#1     4           [11 × 10]
#2     6            [7 × 10]
#3     8           [14 × 10]

You can also do the same without grouping.

mtcars %>% nest_by(cyl, .key = "my_name")
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
2

You can use group_cols() to refer to grouping variables:

mtcars %>% group_by(cyl) %>% nest(my_name = !group_cols())
#> # A tibble: 3 x 2
#> # Groups:   cyl [3]
#>     cyl my_name
#>   <dbl> <list>
#> 1     6 <tibble [7 × 10]>
#> 2     4 <tibble [11 × 10]>
#> 3     8 <tibble [14 × 10]>

mtcars %>% nest(my_name = !cyl)
#> # A tibble: 3 x 2
#> # Groups:   cyl [3]
#>     cyl my_name
#>   <dbl> <list>
#> 1     6 <tibble [7 × 10]>
#> 2     4 <tibble [11 × 10]>
#> 3     8 <tibble [14 × 10]>
Lionel Henry
  • 6,652
  • 27
  • 33
1

The name can be provided directly in the arguments of nest:

mtcars %>% nest( my_name = -cyl )    # Nest by everything except cyl
#  # A tibble: 3 x 2
#      cyl my_name
#    <dbl> <list>
#  1     6 <tibble [7 × 10]>
#  2     4 <tibble [11 × 10]>
#  3     8 <tibble [14 × 10]>
Artem Sokolov
  • 13,196
  • 4
  • 43
  • 74
  • This works but seems to introduce some redundancy as it requires repeating in the argument of nest() all the grouping variables. – danilinares Jun 09 '20 at 21:29