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)
As the
colsargument 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 incolswe get:I think it would be nice to allow renaming via
colsCreated on 2021-03-08 by the reprex package (v1.0.0)