Skip to content
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

unnest_longer() uses a mix of keep_empty = TRUE and keep_empty = FALSE #1363

Closed
DavisVaughan opened this issue Jun 2, 2022 · 0 comments · Fixed by #1442
Closed

unnest_longer() uses a mix of keep_empty = TRUE and keep_empty = FALSE #1363

DavisVaughan opened this issue Jun 2, 2022 · 0 comments · Fixed by #1442
Assignees
Labels
bug an unexpected problem or unintended behavior
Milestone

Comments

@DavisVaughan
Copy link
Member

DavisVaughan commented Jun 2, 2022

Currently it keeps NULL elements, but drops empty vectors like integer().

It should work like unchop(keep_empty = FALSE) which drops both NULL and integer().

This is heavily related to #1339. I imagine that to be able to add keep_empty to unnest_longer() we will have to resolve this. I think it requires changes to tidyr:::elt_to_long()'s handling of NULL. When keep_empty = FALSE (the default) it should probably use unspecified(0L) and an empty vector for the index if needed. The current behavior looks to be what should happen for keep_empty = TRUE.

Fixing this is probably a small breaking change. But I think it is definitely worth it for package consistency.

library(tidyr)

df <- tibble(
  x = list(NULL, 1L, integer())
)

df
#> # A tibble: 3 × 1
#>   x        
#>   <list>   
#> 1 <NULL>   
#> 2 <int [1]>
#> 3 <int [0]>

# Inconsistent:
# Keeps the `NULL` as `NA`.
# Drops the `integer()`.
unnest_longer(df, x)
#> # A tibble: 2 × 1
#>       x
#>   <int>
#> 1    NA
#> 2     1

# Consistent with our definition of "empty".
# - Drops `NULL`
# - Drops `integer()` (size 0 vectors)
unchop(df, x)
#> # A tibble: 1 × 1
#>       x
#>   <int>
#> 1     1

# - Keeps `NULL` as NA
# - Keeps `integer()` as NA
unchop(df, x, keep_empty = TRUE)
#> # A tibble: 3 × 1
#>       x
#>   <int>
#> 1    NA
#> 2     1
#> 3    NA

Created on 2022-06-02 by the reprex package (v2.0.1)

@DavisVaughan DavisVaughan added the bug an unexpected problem or unintended behavior label Jun 2, 2022
@DavisVaughan DavisVaughan self-assigned this Jan 3, 2023
@DavisVaughan DavisVaughan added this to the v1.3.0 milestone Jan 3, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug an unexpected problem or unintended behavior
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant