tidyr icon indicating copy to clipboard operation
tidyr copied to clipboard

Should `indices_to` go to left of value column?

Open hadley opened this issue 2 years ago • 1 comments

In unnest_longer()? Since you normally expect the name of something to come first.

hadley avatar Feb 16 '23 15:02 hadley

Seems reasonable to me

DavisVaughan avatar Feb 16 '23 17:02 DavisVaughan

This seems like a good tidy dev day issue, I think you'd need to look at new_long_indexed_frame() in particular, and make sure tests get updated to account for the new order, and add a NEWS bullet.

Here are some examples of what we mean by this:

library(tidyr)

df <- tibble(
  x = 1:4,
  y = list(NULL, 1:3, 4:5, integer())
)

# i.e. should `index` come before `y`?
df |> unnest_longer(y, indices_to = "index")
#> # A tibble: 5 × 3
#>       x     y index
#>   <int> <int> <int>
#> 1     2     1     1
#> 2     2     2     2
#> 3     2     3     3
#> 4     3     4     1
#> 5     3     5     2


# more examples

df <- tibble(
  x = 1:2,
  y = list(1:2, 3:4),
  z = list(5:6, 7:8)
)
df |>
  unnest_longer(c(y, z), indices_to = "{col}_id")
#> # A tibble: 4 × 5
#>       x     y  y_id     z  z_id
#>   <int> <int> <int> <int> <int>
#> 1     1     1     1     5     1
#> 2     1     2     2     6     2
#> 3     2     3     1     7     1
#> 4     2     4     2     8     2


df <- tibble(
  x = 1:2,
  y = list(c(a = 1, b = 2), c(a = 10, b = 11, c = 12))
)
df |> unnest_longer(y, indices_to = "foo")
#> # A tibble: 5 × 3
#>       x     y foo  
#>   <int> <dbl> <chr>
#> 1     1     1 a    
#> 2     1     2 b    
#> 3     2    10 a    
#> 4     2    11 b    
#> 5     2    12 c

DavisVaughan avatar Sep 08 '25 17:09 DavisVaughan