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() regression with tibble 3.0.1 #949

Closed
krlmlr opened this issue Apr 29, 2020 · 4 comments
Closed

pivot_longer() regression with tibble 3.0.1 #949

krlmlr opened this issue Apr 29, 2020 · 4 comments
Milestone

Comments

@krlmlr
Copy link
Member

@krlmlr krlmlr commented Apr 29, 2020

From tidyverse/tibble#766, CC @mvazquezs.

Is the previous behavior correct? Do we need a better error message?

tibble 2.1.3

library(tidyverse)

tibble(id = 1, x_1 = 1, x_2 = 2, y_1 = 1, y_2 = 2) %>%
  pivot_longer(
    cols = -id,
    names_to = c("x", "x_dil"),
    names_pattern = "(.+)_(.+)",
    values_to = c("mfi", "dil")
  )
#> # A tibble: 4 x 5
#>      id x     x_dil   mfi   dil
#>   <dbl> <chr> <chr> <dbl> <dbl>
#> 1     1 x     1         1    NA
#> 2     1 x     2        NA     2
#> 3     1 y     1         1    NA
#> 4     1 y     2        NA     2

Created on 2020-04-29 by the reprex package (v0.3.0)

tibble 3.0.1

library(tidyverse)

tibble(id = 1, x_1 = 1, x_2 = 2, y_1 = 1, y_2 = 2) %>%
  pivot_longer(
    cols = -id,
    names_to = c("x", "x_dil"),
    names_pattern = "(.+)_(.+)",
    values_to = c("mfi", "dil")
  )
#> Error: Assigned data `values_to` must be compatible with existing data.
#> x Existing data has 4 rows.
#> x Assigned data has 2 rows.
#> ℹ Only vectors of size 1 are recycled.

Created on 2020-04-29 by the reprex package (v0.3.0)

@hadley hadley added this to the v1.1.0 milestone Apr 29, 2020
@hadley
Copy link
Member

@hadley hadley commented Apr 29, 2020

Almost certainly a bug. But should be simple fix.

@hadley
Copy link
Member

@hadley hadley commented May 4, 2020

Looking at the code, I don't think there's any suggestion that values_to should be a vector, so I think this can just be a better error.

@hadley
Copy link
Member

@hadley hadley commented May 4, 2020

I think the call should be more like this:

library(tidyr)

tibble(id = 1, x_mfi = 1, x_dil = 2, y_mfi = 1, y_dil = 2) %>%
  pivot_longer(
    cols = -id,
    names_to = c("x", ".value"),
    names_pattern = "(.+)_(.+)"
  )
#> # A tibble: 2 x 4
#>      id x       mfi   dil
#>   <dbl> <chr> <dbl> <dbl>
#> 1     1 x         1     2
#> 2     1 y         1     2

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

@hadley hadley closed this as completed in 12e7b24 May 4, 2020
@mvazquezs
Copy link

@mvazquezs mvazquezs commented May 4, 2020

The special .values name works fine and simplifies the code a lot. Thanks so much @hadley and @krlmlr for your help.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants