Skip to content

Allow unnest with list columns of differing lengths? #328

@karldw

Description

@karldw

unnest currently can't handle multiple list columns with different lengths. If the user requests an unnesting of one list column from a dataframe with multiple, unnest will fail if the number of elements differs. Would it be possible for unnest to copy the other list columns, just as it copies values from standard, atomic columns?

In particular, the current behavior means unnest doesn't work with sf data, since the geometry column is already a list column.

library(sf)
library(dplyr)
library(tidyr)
nc <- st_read(system.file("shape/nc.shp", package = "sf")) %>% 
  slice(1:3) %>%
  select(NAME) %>%
  mutate(y = strsplit(c("a", "d,e,f", "g,h"), ","))

nc
#> Simple feature collection with 3 features and 2 fields
#> geometry type:  MULTIPOLYGON
#> dimension:      XY
#> bbox:           xmin: -84.32385 ymin: 33.88199 xmax: -75.45698 ymax: 36.58965
#> epsg (SRID):    4267
#> proj4string:    +proj=longlat +datum=NAD27 +no_defs
#> # A tibble: 3 x 3
#>        NAME         y          geometry
#>      <fctr>    <list>  <simple_feature>
#> 1      Ashe <chr [1]> <MULTIPOLYGON...>
#> 2 Alleghany <chr [3]> <MULTIPOLYGON...>
#> 3     Surry <chr [2]> <MULTIPOLYGON...>


# Current behavior:
unnest(nc, y, .drop = FALSE)
#>  Error: All nested columns must have the same number of elements.


# Expected behavior: values in geometry column copied for the newly-created rows
unnest(nc, y, .drop = FALSE)
#> # A tibble: 6 x 2
#>        NAME     y           geometry
#>      <fctr> <chr>   <simple_feature>
#> 1      Ashe     a  <MULTIPOLYGON...>
#> 2 Alleghany     d  <MULTIPOLYGON...>
#> 3 Alleghany     e  <MULTIPOLYGON...>
#> 4 Alleghany     f  <MULTIPOLYGON...>
#> 5     Surry     g  <MULTIPOLYGON...>
#> 6     Surry     h  <MULTIPOLYGON...>

Ref: r-spatial/sf#426

Metadata

Metadata

Assignees

No one assigned

    Labels

    featurea feature request or enhancementrectangling 🗄️converting deeply nested lists into tidy data frames

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions