Tidyr: Return tibble when nesting

Created on 17 Jul 2018  路  2Comments  路  Source: tidyverse/tidyr

Currently, mtcars %>% group_by(am, cyl) %>% nest() returns a tibble whereas mtcars %>% nest(-am, -cyl) returns a data.frame with list-cols, which doesn't print well.

Since tibble is already a dependency of tidyr, I imagine that it could be as simple as adding as_tibble to the beginning of nest.data.frame but perhaps there's a more elegant method?

Coercing the _output_ to a tibble results in a list-col of data.frames, whereas coercing the _input_ returns a list-col of tibbles:

library(dplyr)
library(tidyr)

mtcars %>% nest(-am, -cyl) %>% as_tibble()
#> # A tibble: 6 x 3
#>     cyl    am data                 
#>   <dbl> <dbl> <list>               
#> 1    6.    1. <data.frame [3 x 9]> 
#> 2    4.    1. <data.frame [8 x 9]> 
#> 3    6.    0. <data.frame [4 x 9]> 
#> 4    8.    0. <data.frame [12 x 9]>
#> 5    4.    0. <data.frame [3 x 9]> 
#> 6    8.    1. <data.frame [2 x 9]>

mtcars %>% as_tibble() %>% nest(-am, -cyl)
#> # A tibble: 6 x 3
#>     cyl    am data             
#>   <dbl> <dbl> <list>           
#> 1    6.    1. <tibble [3 x 9]> 
#> 2    4.    1. <tibble [8 x 9]> 
#> 3    6.    0. <tibble [4 x 9]> 
#> 4    8.    0. <tibble [12 x 9]>
#> 5    4.    0. <tibble [3 x 9]> 
#> 6    8.    1. <tibble [2 x 9]>
feature rectangling tidy-dev-day

Most helpful comment

I'll take a swing at this as part of tidy-dev-day 2019

All 2 comments

Agreed that this should return a tibble, and that your diagnosis of the needed fix appears correct to me.

I'll take a swing at this as part of tidy-dev-day 2019

Was this page helpful?
0 / 5 - 0 ratings

Related issues

davidhunterwalsh picture davidhunterwalsh  路  4Comments

coatless picture coatless  路  6Comments

mindymallory picture mindymallory  路  3Comments

albertotb picture albertotb  路  7Comments

andrewpbray picture andrewpbray  路  8Comments