Skip to content

Unnesting tibble with zero rows removes unnested column #483

Closed

Description

@frederikziebell

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.

Activity

  1. krlmlr commented on Sep 13, 2018

    @krlmlr
    Member

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

  2. added
    bugan unexpected problem or unintended behavior
    rectangling 🗄️converting deeply nested lists into tidy data frames
    on Jan 4, 2019
  3. hadley commented on Jan 4, 2019

    @hadley
  4. frederikziebell commented on Jan 4, 2019

    @frederikziebell
    Author
  5. hadley commented on Jan 4, 2019

    @hadley
  6. khailper commented on Jan 19, 2019

    @khailper
    Contributor

    I'll try this.

  7. added a commit that references this issue on Jan 19, 2019
    fa06c5e
  8. hadley commented on Jan 22, 2019

    @hadley
    Member

    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)

  9. hadley commented on Jan 22, 2019

    @hadley
    Member

    I think the root cause is actually this:

    list_col_type(character())
    #> [1] "data.frame"
  10. added a commit that references this issue on Mar 27, 2026
    f87fd51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugan unexpected problem or unintended behaviorrectangling 🗄️converting deeply nested lists into tidy data framestidy-dev-day 🤓Tidyverse Developer Day rstd.io/tidy-dev-day

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions