Skip to content

Allow pivot_longer(names_to = NULL) to produce no name columns #961

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

Closed
mitchelloharawild opened this issue May 15, 2020 · 1 comment · Fixed by #962
Closed

Allow pivot_longer(names_to = NULL) to produce no name columns #961

mitchelloharawild opened this issue May 15, 2020 · 1 comment · Fixed by #962
Labels
feature a feature request or enhancement help wanted ❤️ we'd love your help! pivoting ♻️ pivot rectangular data to different "shapes"

Comments

@mitchelloharawild
Copy link
Contributor

A suggestion. Sometimes the column names provide no new/useful information as the values encode the same (or better) information. In this case, the names column is unwanted and dropped soon after the pivot.

If names_to can create one or more columns based on patterns, could it be extended to allow 0 columns to simplify the pivot_longer(names_to = "junk") %>% select(-junk) pattern?

@hadley hadley added feature a feature request or enhancement pivoting ♻️ pivot rectangular data to different "shapes" labels May 15, 2020
@hadley
Copy link
Member

hadley commented May 15, 2020

Do you want to have a go at a PR? I think it's just a matter of adding another branch to build_longer_spec() (using new_tibble() to generate a tibble with 0 columns and the correct number of rows) because it already works if you create the spec yourself:

library(tidyr)

df <- data.frame(x = 1, y = 2, z = 3)

spec <- build_longer_spec(df, everything())
pivot_longer_spec(df, spec)
#> # A tibble: 3 x 2
#>   name  value
#>   <chr> <dbl>
#> 1 x         1
#> 2 y         2
#> 3 z         3

spec$name <- NULL
pivot_longer_spec(df, spec)
#> # A tibble: 3 x 1
#>   value
#>   <dbl>
#> 1     1
#> 2     2
#> 3     3

Created on 2020-05-15 by the reprex package (v0.3.0)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature a feature request or enhancement help wanted ❤️ we'd love your help! pivoting ♻️ pivot rectangular data to different "shapes"
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants