Search code examples
rdplyrtidyselect

Since .key is deprecated how it is possible to rename the data column in nest()?


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.


Solution

  • 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")