Skip to content

We can't use unnest_wider() on vctrs list columns #741

@moodymudskipper

Description

@moodymudskipper

See #737 for full example

These are the columns that pivot_wider() creates so I don't believe this is expected :

library(tidyr)

df1 <- tibble::tribble(
  ~type,          ~name, ~var1,
  "Country",   "Norway", 169L, 
    "Sport",     "Skii", 169L, 
  "Country",    "Spain", 150L, 
    "Sport",     "Bike", 150L, 
    "Sport",   "Soccer", 150L, 
    "Sport",   "Basket", 150L, 
  "Country",      "USA",   0L, 
    "Sport", "Baseball",   0L, 
)

df2 <- pivot_wider(df1, names_from = "type", values_from = "name") 
#> Warning: Values in `name` are not uniquely identified; output will contain list-cols.
#> * Use `values_fn = list(name = list)` to suppress this warning.
#> * Use `values_fn = list(name = length)` to identify where the duplicates arise
#> * Use `values_fn = list(name = summary_fun)` to summarise duplicates
df2
#> # A tibble: 3 x 3
#>    var1     Country       Sport
#>   <int> <list<chr>> <list<chr>>
#> 1   169         [1]         [1]
#> 2   150         [1]         [3]
#> 3     0         [1]         [1]

unnest_wider(df2, Sport)
#> New names:
#> * `` -> ...1
#> New names:
#> * `` -> ...1
#> * `` -> ...2
#> * `` -> ...3
#> New names:
#> * `` -> ...1
#> No common type for `x` <tbl_df<...1:character>> and `y` <character>.

df2$Sport <- unclass(df2$Sport)

unnest_wider(df2, Sport)
#> New names:
#> * `` -> ...1
#> New names:
#> * `` -> ...1
#> * `` -> ...2
#> * `` -> ...3
#> New names:
#> * `` -> ...1
#> # A tibble: 3 x 5
#>    var1     Country ...1     ...2   ...3  
#>   <int> <list<chr>> <chr>    <chr>  <chr> 
#> 1   169         [1] Skii     <NA>   <NA>  
#> 2   150         [1] Bike     Soccer Basket
#> 3     0         [1] Baseball <NA>   <NA>

pivot_wider(df1, names_from = "type", values_from = "name", values_fn = list(name=list))
#> # A tibble: 3 x 3
#>    var1     Country       Sport
#>   <int> <list<chr>> <list<chr>>
#> 1   169         [1]         [1]
#> 2   150         [1]         [3]
#> 3     0         [1]         [1]

Created on 2019-09-15 by the reprex package (v0.3.0)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions