-
Notifications
You must be signed in to change notification settings - Fork 417
Closed
Labels
featurea feature request or enhancementa feature request or enhancementpivoting ♻️pivot rectangular data to different "shapes"pivot rectangular data to different "shapes"
Description
See #737 for full example
library(tidyr)
df1 <- tibble::tribble(
~Sport,
"Skii",
c("Bike", "Soccer", "Basket"),
"Baseball"
)
####################
# This makes sense #
####################
unnest_wider(df1, Sport)
#> New names:
#> * `` -> ...1
#> New names:
#> * `` -> ...1
#> * `` -> ...2
#> * `` -> ...3
#> New names:
#> * `` -> ...1
#> # A tibble: 3 x 3
#> ...1 ...2 ...3
#> <chr> <chr> <chr>
#> 1 Skii <NA> <NA>
#> 2 Bike Soccer Basket
#> 3 Baseball <NA> <NA>
#########################################################################
# but here .. are not needed, and I doubt many would want the following #
#########################################################################
unnest_wider(df1, Sport, names_sep = "_")
#> New names:
#> * `` -> ...1
#> New names:
#> * `` -> ...1
#> * `` -> ...2
#> * `` -> ...3
#> New names:
#> * `` -> ...1
#> # A tibble: 3 x 3
#> Sport_...1 Sport_...2 Sport_...3
#> <chr> <chr> <chr>
#> 1 Skii <NA> <NA>
#> 2 Bike Soccer Basket
#> 3 Baseball <NA> <NA>
############################################
# can be solved but complex and not robust #
############################################
unnest_wider(df1, Sport, names_sep = "_",
names_repair = ~sub("..." , "", ., fixed=TRUE))
#> New names:
#> * `` -> ...1
#> New names:
#> * `` -> ...1
#> * `` -> ...2
#> * `` -> ...3
#> New names:
#> * `` -> ...1
#> # A tibble: 3 x 3
#> Sport_1 Sport_2 Sport_3
#> <chr> <chr> <chr>
#> 1 Skii <NA> <NA>
#> 2 Bike Soccer Basket
#> 3 Baseball <NA> <NA>
Created on 2019-09-15 by the reprex package (v0.3.0)
I had to use this trick here and here and I don't see a simple way to do it.
I think it's a common case and it would be nice to have an intuitive way of sorting it out.
Metadata
Metadata
Assignees
Labels
featurea feature request or enhancementa feature request or enhancementpivoting ♻️pivot rectangular data to different "shapes"pivot rectangular data to different "shapes"