Skip to content

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

@moodymudskipper

Description

@moodymudskipper

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

No one assigned

    Labels

    featurea feature request or enhancementpivoting ♻️pivot rectangular data to different "shapes"

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions