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_wider() with values_from a data frame column #926

Closed
mikmart opened this issue Apr 9, 2020 · 0 comments
Closed

pivot_wider() with values_from a data frame column #926

mikmart opened this issue Apr 9, 2020 · 0 comments
Labels
bug an unexpected problem or unintended behavior pivoting ♻️ pivot rectangular data to different "shapes"
Milestone

Comments

@mikmart
Copy link

mikmart commented Apr 9, 2020

It seems like values get mixed up and replicated when pivoting to wider from a data frame column:

library(tidyr)

tbl <- tibble(
  i = c(1, 2, 1, 2),
  g = c("a", "a", "b", "b"),
  d = tibble(x = 1:4, y = 5:8)
)

tbl
#> # A tibble: 4 x 3
#>       i g       d$x    $y
#>   <dbl> <chr> <int> <int>
#> 1     1 a         1     5
#> 2     2 a         2     6
#> 3     1 b         3     7
#> 4     2 b         4     8

tbl %>% 
  pivot_wider(
    names_from = g,
    values_from = d
  )
#> # A tibble: 2 x 3
#>       i   a$x    $y   b$x    $y
#>   <dbl> <int> <int> <int> <int>
#> 1     1     1     5     2     6
#> 2     2     1     5     2     6

# expected to get
tibble(
  i = c(1, 2),
  a = tibble(x = 1:2, y = 5:6),
  b = tibble(x = 3:4, y = 7:8)
)
#> # A tibble: 2 x 3
#>       i   a$x    $y   b$x    $y
#>   <dbl> <int> <int> <int> <int>
#> 1     1     1     5     3     7
#> 2     2     2     6     4     8
@hadley hadley added bug an unexpected problem or unintended behavior pivoting ♻️ pivot rectangular data to different "shapes" labels Apr 21, 2020
@hadley hadley added this to the v1.1.0 milestone Apr 21, 2020
@hadley hadley closed this as completed in c6278a8 Apr 21, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug an unexpected problem or unintended behavior pivoting ♻️ pivot rectangular data to different "shapes"
Projects
None yet
Development

No branches or pull requests

2 participants