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 tibble with zero rows removes unnested column #483

Closed
frederikziebell opened this Issue Aug 1, 2018 · 7 comments

Comments

Projects
None yet
4 participants
@frederikziebell
Copy link

frederikziebell commented Aug 1, 2018

Here is an example that produces the problem:

df <- tibble(
 a = list(c(1, 2), 3),
 b = c(4,5)
)

df %>%
  unnest(a) %>%
  colnames
# [1] "b" "a"

df %>%
  filter(b==1) %>%
  unnest(a) %>%
  colnames
# [1] "b"

If the special case occurs that the tibble being unnested has no rows, all code acting on the unnested column breaks.

@krlmlr

This comment has been minimized.

Copy link
Member

krlmlr commented Sep 13, 2018

Thanks, confirmed with both the CRAN version and 581c2c0.

@hadley

This comment was marked as resolved.

Copy link
Member

hadley commented Jan 4, 2019

I see an error (possible due to dev dplyr)

library(tidyr)

df <- tibble::tibble(
 a = list(c(1, 2), 3),
 b = c(4,5)
)

df %>%
  unnest(a) %>%
  names()
#> [1] "b" "a"

df %>%
  filter(b==1) %>%
  unnest(a) %>%
  names()
#> Error in data.matrix(data): (list) object cannot be coerced to type 'double'

Created on 2019-01-04 by the reprex package (v0.2.1.9000)

@frederikziebell

This comment was marked as off-topic.

Copy link
Author

frederikziebell commented Jan 4, 2019

I get the same error. Here is my sessionInfo:

R version 3.5.0 (2018-04-23)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows >= 8 x64 (build 9200)

Matrix products: default

locale:
[1] LC_COLLATE=German_Germany.1252  LC_CTYPE=German_Germany.1252    LC_MONETARY=German_Germany.1252 LC_NUMERIC=C                   
[5] LC_TIME=German_Germany.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] tidyr_0.8.2

loaded via a namespace (and not attached):
 [1] Rcpp_0.12.19     crayon_1.3.4     dplyr_0.7.7      assertthat_0.2.0 R6_2.3.0         magrittr_1.5     pillar_1.3.0     rlang_0.3.0.1   
 [9] rstudioapi_0.8   bindrcpp_0.2.2   tools_3.5.0      glue_1.3.0       purrr_0.2.5      yaml_2.2.0       compiler_3.5.0   pkgconfig_2.0.2 
[17] bindr_0.1.1      knitr_1.20       tidyselect_0.2.5 tibble_1.4.2  
@hadley

This comment was marked as resolved.

Copy link
Member

hadley commented Jan 4, 2019

But I see the same basic problem with a different example:

library(tidyr)
data.frame(a = character(), b = character()) %>% 
  unnest(b) %>% 
  names()
#> [1] "a"

Created on 2019-01-04 by the reprex package (v0.2.1.9000)

@khailper

This comment has been minimized.

Copy link
Contributor

khailper commented Jan 19, 2019

I'll try this.

khailper added a commit to khailper/tidyr that referenced this issue Jan 19, 2019

@hadley

This comment has been minimized.

Copy link
Member

hadley commented Jan 22, 2019

A bit more of a reprex to help me understand the problem:

library(tidyr)

df <- tibble::tibble(a = list(1), y = 1)
df %>% unnest(a) %>% str()
#> Classes 'tbl_df', 'tbl' and 'data.frame':    1 obs. of  2 variables:
#>  $ y: num 1
#>  $ a: num 1
df %>% .[0, ] %>% unnest(a) %>% str()
#> Classes 'tbl_df', 'tbl' and 'data.frame':    0 obs. of  1 variable:
#>  $ y: num

Created on 2019-01-22 by the reprex package (v0.2.1.9000)

@hadley

This comment has been minimized.

Copy link
Member

hadley commented Jan 22, 2019

I think the root cause is actually this:

list_col_type(character())
#> [1] "data.frame"

@hadley hadley closed this in f87fd51 Jan 22, 2019

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