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

pivot_wider() has an issue with 0-row data frames when a manual spec specifies a nonexistent key #1250

Closed
DavisVaughan opened this issue Dec 2, 2021 · 1 comment · Fixed by #1253
Labels
bug an unexpected problem or unintended behavior pivoting ♻️ pivot rectangular data to different "shapes"

Comments

@DavisVaughan
Copy link
Member

library(tidyr)

spec <- tibble(.name = "Release", .value = "seen", station = fish_encounters$station[1])
spec
#> # A tibble: 1 × 3
#>   .name   .value station
#>   <chr>   <chr>  <fct>  
#> 1 Release seen   Release

pivot_wider_spec(
  fish_encounters[0,], 
  spec
)
#> Error: Can't subset elements that don't exist.
#> x Location 1 doesn't exist.
#> ℹ There are only 0 elements.

# This kind of thing generally works and gives NA
pivot_wider_spec(
  fish_encounters[2,], 
  spec
)
#> # A tibble: 1 × 2
#>   fish  Release
#>   <fct>   <int>
#> 1 4842       NA

I think this is just due to an error in the way wrap_vec() computes the slice indices

@DavisVaughan DavisVaughan added bug an unexpected problem or unintended behavior pivoting ♻️ pivot rectangular data to different "shapes" labels Dec 3, 2021
@DavisVaughan
Copy link
Member Author

Slightly more minimal reprex

library(tidyr)

spec <- tibble(.name = "name", .value = "value", x = 1L)

df <- tibble(key = "a", value = 1L, x = 2L)
pivot_wider_spec(df, spec)
#> # A tibble: 1 × 2
#>   key    name
#>   <chr> <int>
#> 1 a        NA

df <- tibble(key = character(), value = integer(), x = integer())
pivot_wider_spec(df, spec, id_cols = key)
#> Error: Can't subset elements that don't exist.
#> x Location 1 doesn't exist.
#> ℹ There are only 0 elements.

Created on 2021-12-03 by the reprex package (v2.0.1)

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 pivoting ♻️ pivot rectangular data to different "shapes"
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant