Skip to content

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

@DavisVaughan

Description

@DavisVaughan

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)

Metadata

Metadata

Assignees

Labels

bugan unexpected problem or unintended behavior

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions