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

The .. used when repairing names are not necessary when there is a prefix #742

Closed
moodymudskipper opened this issue Sep 15, 2019 · 1 comment
Closed
Labels
feature pivoting ♻️

Comments

@moodymudskipper
Copy link

@moodymudskipper moodymudskipper commented Sep 15, 2019

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.

@hadley
Copy link
Member

@hadley hadley commented Nov 24, 2019

Minimal reprex:

library(tidyr)

df <- tibble(
  x = list("Skii", c("Bike", "Soccer", "Basket"), "Baseball")
)
unnest_wider(df, x, names_sep = "_")
#> New names:
#> * `` -> ...1
#> New names:
#> * `` -> ...1
#> * `` -> ...2
#> * `` -> ...3
#> New names:
#> * `` -> ...1
#> # A tibble: 3 x 3
#>   x_...1   x_...2 x_...3
#>   <chr>    <chr>  <chr> 
#> 1 Skii     <NA>   <NA>  
#> 2 Bike     Soccer Basket
#> 3 Baseball <NA>   <NA>

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

@hadley hadley added feature pivoting ♻️ labels Nov 24, 2019
@hadley hadley closed this as completed in 740f266 Nov 24, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature pivoting ♻️
Projects
None yet
Development

No branches or pull requests

2 participants