Skip to content

Should unnest_wider() default to some separator for unnamed entries? #1367

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

Closed
hadley opened this issue Jun 6, 2022 · 3 comments · Fixed by #1462
Closed

Should unnest_wider() default to some separator for unnamed entries? #1367

hadley opened this issue Jun 6, 2022 · 3 comments · Fixed by #1462
Assignees
Labels
feature a feature request or enhancement nesting 🐦 nesting, chopping, and packing
Milestone

Comments

@hadley
Copy link
Member

hadley commented Jun 6, 2022

I can't imagine that this behaviour is ever useful:

library(tidyr)

df <- tribble(
  ~x, ~y,
  1, c(11, 12, 13),
  2, 21
)
df |> unnest_wider(y)
#> New names:
#> New names:
#> • `` -> `...1`
#> • `` -> `...2`
#> • `` -> `...3`
#> # A tibble: 2 × 4
#>       x  ...1  ...2  ...3
#>   <dbl> <dbl> <dbl> <dbl>
#> 1     1    11    12    13
#> 2     2    21    NA    NA

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

What if it was instead:

df |> unnest_wider(y)
#> Warning: 
#> * Using unnest_wider() with column containing unnamed data
#> i Use `names_sep = "_"` to suppress this warning and control the separator.
#> # A tibble: 2 × 4
#>       x   y_1   y_2   y_3
#>   <dbl> <dbl> <dbl> <dbl>
#> 1     1    11    12    13
#> 2     2    21    NA    NA

?

@DavisVaughan DavisVaughan added the feature a feature request or enhancement label Jun 6, 2022
@hadley
Copy link
Member Author

hadley commented Jun 6, 2022

And could it recommend names_sep instead of (as well as?) names_repair here?

library(tidyr)

df <- tibble(x = 1, y = list(list(x = 2)))
df |> unnest_wider(y)
#> Error in `unpack()`:
#> ! Names must be unique.
#> ✖ These names are duplicated:
#>   * "x" at locations 1 and 2.
#> ℹ Use argument `names_repair` to specify repair strategy.

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

@hadley
Copy link
Member Author

hadley commented Jun 8, 2022

I tried to do the first one, and it's a bit more complicated than I thought. You need something like this in unnest_wider()

  internal_names <- map_lgl(data[cols], function(x) all(map_lgl(x, ~ length(.x) == 0 || !is.null(names(.x)))))
  if (is.null(names_sep) && any(!internal_names)) {
    warn("List-column lacks internal names, setting `names_sep = '_'")
    names_sep <- "_"
  }

But that fails to account for df-cols.

@hadley
Copy link
Member Author

hadley commented Nov 18, 2022

Need to do the same thing for separate_wider_*()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature a feature request or enhancement nesting 🐦 nesting, chopping, and packing
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants