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

Unnesting list-columns gave error even they all have equal lengths #460

Closed
yuanwxu opened this Issue Jun 5, 2018 · 3 comments

Comments

Projects
None yet
4 participants
@yuanwxu
Copy link

yuanwxu commented Jun 5, 2018

Here is an example that reproduces the error.

> df <- tibble(x = c("a", "b", "c"), y = list(a = 1:3, b = 4:6, c = 7:9), z = list(1:3))
> df
# A tibble: 3 x 3
  x     y         z        
  <chr> <list>    <list>   
1 a     <int [3]> <int [3]>
2 b     <int [3]> <int [3]>
3 c     <int [3]> <int [3]>
> unnest(df)
Error: All nested columns must have the same number of elements.

However, I note that slice the whole data frame and then unnest works

> df %>% slice(1:n()) %>% unnest()
# A tibble: 9 x 3
  x         y     z
  <chr> <int> <int>
1 a         1     1
2 a         2     2
3 a         3     3
4 b         4     1
5 b         5     2
6 b         6     3
7 c         7     1
8 c         8     2
9 c         9     3
@markdly

This comment has been minimized.

Copy link
Contributor

markdly commented Jun 7, 2018

Hi @yuanwxu, just another community member here.
Here's a slightly more minimal reprex of what I think the issue is:

Issue

library(tidyverse)
df1 <- tibble(x = c("a"), y = list(1:3), z = list(4:6))
df1 %>% unnest()
#> # A tibble: 3 x 3
#>   x         y     z
#>   <chr> <int> <int>
#> 1 a         1     4
#> 2 a         2     5
#> 3 a         3     6

# add name to list in y causes issue
df2 <- tibble(x = c("a"), y = list(foo = 1:3), z = list(4:6))
df2 %>% unnest()
#> Error: All nested columns must have the same number of elements.

Workaround

Another possible workaround instead of slicing could be to unname the list columns first

df2 %>% mutate_if(is.list, unname) %>% unnest()
#> # A tibble: 3 x 3
#>   x         y     z
#>   <chr> <int> <int>
#> 1 a         1     4
#> 2 a         2     5
#> 3 a         3     6

Why is this happening?
Looking at the code, I think this behaviour comes from

tidyr/R/unnest.R

Lines 96 to 100 in cbdd14e

nested <- dplyr::transmute(dplyr::ungroup(data), !!! quos)
n <- map(nested, function(x) map_int(x, NROW))
if (length(unique(n)) != 1) {
abort("All nested columns must have the same number of elements.")
}

If we adapt that for our example we can see why the error message occurs:

# lengths considered the same
n1 <- df1 %>% select(-x) %>% map(~ map_int(., NROW))
n1
#> $y
#> [1] 3
#> 
#> $z
#> [1] 3
length(unique(n1))
#> [1] 1

# lengths considered different
n2 <- df2 %>% select(-x) %>% map(~ map_int(., NROW))
n2
#> $y
#> foo 
#>   3 
#> 
#> $z
#> [1] 3
length(unique(n2))
#> [1] 2
@hadley

This comment has been minimized.

Copy link
Member

hadley commented Jan 4, 2019

Looks like we need to strip the names here.

@hlendway

This comment has been minimized.

Copy link
Contributor

hlendway commented Jan 19, 2019

I will work on this for dev day.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment