-
Notifications
You must be signed in to change notification settings - Fork 417
Closed
Description
See #737 for full example
These are the columns that pivot_wider()
creates so I don't believe this is expected :
library(tidyr)
df1 <- tibble::tribble(
~type, ~name, ~var1,
"Country", "Norway", 169L,
"Sport", "Skii", 169L,
"Country", "Spain", 150L,
"Sport", "Bike", 150L,
"Sport", "Soccer", 150L,
"Sport", "Basket", 150L,
"Country", "USA", 0L,
"Sport", "Baseball", 0L,
)
df2 <- pivot_wider(df1, names_from = "type", values_from = "name")
#> Warning: Values in `name` are not uniquely identified; output will contain list-cols.
#> * Use `values_fn = list(name = list)` to suppress this warning.
#> * Use `values_fn = list(name = length)` to identify where the duplicates arise
#> * Use `values_fn = list(name = summary_fun)` to summarise duplicates
df2
#> # A tibble: 3 x 3
#> var1 Country Sport
#> <int> <list<chr>> <list<chr>>
#> 1 169 [1] [1]
#> 2 150 [1] [3]
#> 3 0 [1] [1]
unnest_wider(df2, Sport)
#> New names:
#> * `` -> ...1
#> New names:
#> * `` -> ...1
#> * `` -> ...2
#> * `` -> ...3
#> New names:
#> * `` -> ...1
#> No common type for `x` <tbl_df<...1:character>> and `y` <character>.
df2$Sport <- unclass(df2$Sport)
unnest_wider(df2, Sport)
#> New names:
#> * `` -> ...1
#> New names:
#> * `` -> ...1
#> * `` -> ...2
#> * `` -> ...3
#> New names:
#> * `` -> ...1
#> # A tibble: 3 x 5
#> var1 Country ...1 ...2 ...3
#> <int> <list<chr>> <chr> <chr> <chr>
#> 1 169 [1] Skii <NA> <NA>
#> 2 150 [1] Bike Soccer Basket
#> 3 0 [1] Baseball <NA> <NA>
pivot_wider(df1, names_from = "type", values_from = "name", values_fn = list(name=list))
#> # A tibble: 3 x 3
#> var1 Country Sport
#> <int> <list<chr>> <list<chr>>
#> 1 169 [1] [1]
#> 2 150 [1] [3]
#> 3 0 [1] [1]
Created on 2019-09-15 by the reprex package (v0.3.0)
Metadata
Metadata
Assignees
Labels
No labels