Skip to content

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

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

Closed
moodymudskipper opened this issue Sep 15, 2019 · 2 comments · Fixed by #745
Closed

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

moodymudskipper opened this issue Sep 15, 2019 · 2 comments · Fixed by #745

Comments

@moodymudskipper
Copy link

moodymudskipper commented Sep 15, 2019

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)

@hadley
Copy link
Member

hadley commented Sep 16, 2019

Can you please work on making your reprexes more minimal? This code gets to the essence of the problem much more quickly:

library(tidyr)

df <- tibble(
  x = 1:2,
  y = vctrs::list_of(c("a", "b"), "c")
)
df %>% unnest_wider(y)
#> New names:
#> * `` -> ...1
#> * `` -> ...2
#> New names:
#> * `` -> ...1
#> No common type for `x` <tbl_df<
#>   ...1: character
#>   ...2: character
#> >> and `y` <character>.
df %>% unnest_longer(y)
#> No common type for `x` <tbl_df<y:character>> and `y` <character>.

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

@DavisVaughan
Copy link
Member

@hadley see the PR before continuing!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants