Skip to content

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

@DavisVaughan

Description

@DavisVaughan
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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugan unexpected problem or unintended behaviorpivoting ♻️pivot rectangular data to different "shapes"

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions