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_longer() needs names_convert argument #763

Closed
stragu opened this issue Sep 26, 2019 · 4 comments
Closed

pivot_longer() needs names_convert argument #763

stragu opened this issue Sep 26, 2019 · 4 comments
Labels
feature a feature request or enhancement pivoting ♻️ pivot rectangular data to different "shapes"

Comments

@stragu
Copy link

@stragu stragu commented Sep 26, 2019

gather() can use the convert argument to automagically recognise if gathered variable names should be stored as something different to a character vector, using type.convert().

Are there any plans to bring back that alternative to the precise, longer-form `names_ptypes` argument? Or did I miss an explanation why it shouldn't?

Maybe something like names_ptypes = "auto", maybe?

@hadley

This comment has been minimized.

@stragu

This comment has been minimized.

@hadley
Copy link
Member

@hadley hadley commented Nov 28, 2019

Ah yes, I was confused — there's no equivalent of convert() and it would be nice to have one. Would presumably be names_convert = TRUE.

library(tidyr)
df <- tribble(
  ~`1987`, ~`1988`, ~`1989`,
  34, 35, 36,
  45, 46, 47,
  52, 53, 54
)

typeof(gather(df, "year", "value")$year)
#> [1] "character"
typeof(gather(df, "year", "value", convert = TRUE)$year)
#> [1] "integer"

Created on 2019-11-27 by the reprex package (v0.3.0)

@hadley hadley added feature a feature request or enhancement pivoting ♻️ pivot rectangular data to different "shapes" labels Nov 28, 2019
@hadley hadley changed the title bring back equivalent to gather()'s convert argument into pivot_longer() pivot_longer() needs names_convert argument Nov 28, 2019
@hadley
Copy link
Member

@hadley hadley commented Nov 28, 2019

No, I was right earlier and I was confused just now 😄

For this case, you need to use names_ptype; if you want to guess automatically, I'd recommend readr::parse_guess():

library(tidyr)
df <- tribble(
  ~`1987`, ~`1988`, ~`1989`,
  34, 35, 36,
  45, 46, 47,
  52, 53, 54
)

df %>% pivot_longer(everything(), names_ptypes = list(name = integer()))
#> # A tibble: 9 x 2
#>    name value
#>   <int> <dbl>
#> 1  1987    34
#> 2  1988    35
#> 3  1989    36
#> 4  1987    45
#> 5  1988    46
#> 6  1989    47
#> 7  1987    52
#> 8  1988    53
#> 9  1989    54

df %>% 
  pivot_longer(everything()) %>% 
  dplyr::mutate(name = readr::parse_guess(name))
#> # A tibble: 9 x 2
#>    name value
#>   <dbl> <dbl>
#> 1  1987    34
#> 2  1988    35
#> 3  1989    36
#> 4  1987    45
#> 5  1988    46
#> 6  1989    47
#> 7  1987    52
#> 8  1988    53
#> 9  1989    54

Created on 2019-11-27 by the reprex package (v0.3.0)

@hadley hadley closed this as completed Nov 28, 2019
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 pivoting ♻️ pivot rectangular data to different "shapes"
Projects
None yet
Development

No branches or pull requests

2 participants