Skip to content

Error on named tidyselections in pivot_longer() and pivot_wider()  #1104

Description

@mgirlich

As the cols argument supports the tidy-select syntax I thought I could use it to rename a column. But this didn't work as expected and when using a named vector in cols we get:

  • a unclear error message when using a data frame
  • a better error message when using a data frame
  • a clearly unwanted result when the name actually exists as a column

I think it would be nice to allow renaming via cols

library(tidyr)

# unclear error message for data frame
data.frame(x = 1, y = 2) %>% 
  pivot_longer(c(z = y))
#> Error in `[.data.frame`(data, cols): undefined columns selected

# better for tibble
tibble(x = 1, y = 2) %>% 
  pivot_longer(c(z = y))
#> Error: Can't subset columns that don't exist.
#> x Column `z` doesn't exist.

# when the name actually exists we get something quite unwanted
data.frame(x = 1, y = 2, z = 3) %>% 
  pivot_longer(c(z = y))
#> # A tibble: 1 x 4
#>       x     y name  value
#>   <dbl> <dbl> <chr> <dbl>
#> 1     1     2 z         3

Created on 2021-03-08 by the reprex package (v1.0.0)

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