Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nest() on grouped_df returns data frame instead of tibble #649

Closed
tmastny opened this issue Jun 11, 2019 · 1 comment
Closed

nest() on grouped_df returns data frame instead of tibble #649

tmastny opened this issue Jun 11, 2019 · 1 comment
Labels
bug an unexpected problem or unintended behavior rectangling 🗄️ converting deeply nested lists into tidy data frames

Comments

@tmastny
Copy link
Contributor

tmastny commented Jun 11, 2019

nest applied to a grouped tibble returns a data.frame.

Related: #475

library(tidyr)
library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
iris_tibble <- as_tibble(iris)
iris_tibble %>%
  group_by(Species) %>%
  nest(Sepal.Length = Sepal.Length) %>%
  class()
#> [1] "data.frame"

iris_tibble %>%
  nest(Sepal.Length = Sepal.Length) %>%
  class()
#> [1] "tbl_df"     "tbl"        "data.frame"
devtools::session_info()
#> ─ Session info ──────────────────────────────────────────────────────────
#>  setting  value                       
#>  version  R version 3.5.1 (2018-07-02)
#>  os       macOS  10.14.4              
#>  system   x86_64, darwin15.6.0        
#>  ui       X11                         
#>  language (EN)                        
#>  collate  en_US.UTF-8                 
#>  ctype    en_US.UTF-8                 
#>  tz       America/Chicago             
#>  date     2019-06-11                  
#> 
#> ─ Packages ──────────────────────────────────────────────────────────────
#>  package     * version     date       lib
#>  assertthat    0.2.1       2019-03-21 [1]
#>  backports     1.1.4       2019-04-10 [1]
#>  callr         3.1.1       2018-12-21 [1]
#>  cli           1.1.0       2019-03-19 [1]
#>  crayon        1.3.4       2017-09-16 [1]
#>  desc          1.2.0       2018-05-01 [1]
#>  devtools      2.0.1       2018-10-26 [1]
#>  digest        0.6.19      2019-05-20 [1]
#>  dplyr       * 0.8.1.9000  2019-06-11 [1]
#>  evaluate      0.12        2018-10-09 [1]
#>  fs            1.3.1       2019-05-06 [1]
#>  glue          1.3.1       2019-03-12 [1]
#>  highr         0.7         2018-06-09 [1]
#>  htmltools     0.3.6       2017-04-28 [1]
#>  knitr         1.21        2018-12-10 [1]
#>  magrittr      1.5.0.9000  2019-01-15 [1]
#>  memoise       1.1.0       2017-04-21 [1]
#>  pillar        1.4.1       2019-05-28 [1]
#>  pkgbuild      1.0.2       2018-10-16 [1]
#>  pkgconfig     2.0.2       2018-08-16 [1]
#>  pkgload       1.0.2       2018-10-29 [1]
#>  prettyunits   1.0.2       2015-07-13 [1]
#>  processx      3.2.1       2018-12-05 [1]
#>  ps            1.3.0       2018-12-21 [1]
#>  purrr         0.3.2.9000  2019-06-11 [1]
#>  R6            2.4.0       2019-02-14 [1]
#>  Rcpp          1.0.1       2019-03-17 [1]
#>  remotes       2.0.2       2018-10-30 [1]
#>  rlang         0.3.99.9003 2019-06-11 [1]
#>  rmarkdown     1.11        2018-12-08 [1]
#>  rprojroot     1.3-2       2018-01-03 [1]
#>  sessioninfo   1.1.1       2018-11-05 [1]
#>  stringi       1.4.3       2019-03-12 [1]
#>  stringr       1.4.0       2019-02-10 [1]
#>  testthat      2.0.1       2018-10-13 [1]
#>  tibble        2.1.3       2019-06-06 [1]
#>  tidyr       * 0.8.3.9000  2019-06-06 [1]
#>  tidyselect    0.2.5       2018-10-11 [1]
#>  usethis       1.4.0       2018-08-14 [1]
#>  vctrs         0.1.0.9004  2019-06-11 [1]
#>  withr         2.1.2       2018-03-15 [1]
#>  xfun          0.4         2018-10-23 [1]
#>  yaml          2.2.0       2018-07-25 [1]
#>  zeallot       0.1.0       2018-01-28 [1]
#>  source                             
#>  CRAN (R 3.5.1)                     
#>  CRAN (R 3.5.2)                     
#>  CRAN (R 3.5.0)                     
#>  CRAN (R 3.5.2)                     
#>  CRAN (R 3.5.0)                     
#>  CRAN (R 3.5.0)                     
#>  CRAN (R 3.5.1)                     
#>  CRAN (R 3.5.2)                     
#>  Github (tidyverse/dplyr@19aa821)   
#>  CRAN (R 3.5.0)                     
#>  CRAN (R 3.5.2)                     
#>  CRAN (R 3.5.2)                     
#>  CRAN (R 3.5.0)                     
#>  CRAN (R 3.5.0)                     
#>  CRAN (R 3.5.1)                     
#>  Github (tidyverse/magrittr@4104d6b)
#>  CRAN (R 3.5.0)                     
#>  CRAN (R 3.5.2)                     
#>  CRAN (R 3.5.0)                     
#>  CRAN (R 3.5.0)                     
#>  CRAN (R 3.5.0)                     
#>  CRAN (R 3.5.0)                     
#>  CRAN (R 3.5.0)                     
#>  CRAN (R 3.5.0)                     
#>  Github (tidyverse/purrr@e4d5539)   
#>  CRAN (R 3.5.1)                     
#>  CRAN (R 3.5.2)                     
#>  CRAN (R 3.5.0)                     
#>  Github (r-lib/rlang@e60f97f)       
#>  CRAN (R 3.5.0)                     
#>  CRAN (R 3.5.0)                     
#>  CRAN (R 3.5.0)                     
#>  CRAN (R 3.5.2)                     
#>  CRAN (R 3.5.2)                     
#>  CRAN (R 3.5.0)                     
#>  CRAN (R 3.5.1)                     
#>  Github (tidyverse/tidyr@7a2b843)   
#>  CRAN (R 3.5.0)                     
#>  CRAN (R 3.5.0)                     
#>  Github (r-lib/vctrs@a2b48fc)       
#>  CRAN (R 3.5.0)                     
#>  CRAN (R 3.5.0)                     
#>  CRAN (R 3.5.0)                     
#>  CRAN (R 3.5.0)                     
#> 
#> [1] /Library/Frameworks/R.framework/Versions/3.5/Resources/library

Created on 2019-06-11 by the reprex package (v0.2.1.9000)

@DavisVaughan
Copy link
Member

I just hit this too while running some of the tidyr unnest() examples. I think the issue is with vec_cbind() in nest.tbl_df(). There isn't a vec_ptype2() method for grouped data frames yet so it just falls back to a data frame. I think that makes this related to tidyverse/dplyr#4426

library(tidyr)
library(vctrs)
library(dplyr, warn.conflicts = FALSE)

vec_ptype2.grouped_df <- function(x, y, ...) {
  UseMethod("vec_ptype2.grouped_df", y)
}

vec_ptype2.grouped_df.data.frame <- function(x, y, ...) {
  inner_type <- vec_ptype2(ungroup(x), y)
  groups <- mutate(group_data(x), .rows = list(integer()))
  new_grouped_df(inner_type, groups)
}

iris %>%
  group_by(Species) %>%
  nest(data = Sepal.Length)
#> # A tibble: 143 x 5
#> # Groups:   Species [3]
#>    Sepal.Width Petal.Length Petal.Width Species           data
#>  *       <dbl>        <dbl>       <dbl> <fct>   <list<df[,1]>>
#>  1         3.5          1.4         0.2 setosa         [1 × 1]
#>  2         3            1.4         0.2 setosa         [1 × 1]
#>  3         3.2          1.3         0.2 setosa         [2 × 1]
#>  4         3.1          1.5         0.2 setosa         [2 × 1]
#>  5         3.6          1.4         0.2 setosa         [1 × 1]
#>  6         3.9          1.7         0.4 setosa         [1 × 1]
#>  7         3.4          1.4         0.3 setosa         [1 × 1]
#>  8         3.4          1.5         0.2 setosa         [2 × 1]
#>  9         2.9          1.4         0.2 setosa         [1 × 1]
#> 10         3.1          1.5         0.1 setosa         [1 × 1]
#> # … with 133 more rows

Created on 2019-07-02 by the reprex package (v0.2.0.9000)

@hadley hadley added bug an unexpected problem or unintended behavior rectangling 🗄️ converting deeply nested lists into tidy data frames labels Jul 22, 2019
@hadley hadley closed this as completed in 0128e77 Jul 22, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug an unexpected problem or unintended behavior rectangling 🗄️ converting deeply nested lists into tidy data frames
Projects
None yet
Development

No branches or pull requests

3 participants