Skip to contents

Rectangling embedded subheaders

Usage

untangle2(df, regex, orig, new)

Arguments

df

A data frame with embedded subheaders.

regex

Regular expression to match the subheaders.

orig

Variable containing the extraneous subheaders.

new

Name of variable that will contain the group values.

Value

A tibble without the matched subheaders and a new variable containing the grouping data.

Details

Special thanks to Jenny Bryan for fixing the initial tidyeval code and overall function structure.

Examples

data(primates2017)
# put taxonomic family in its own variable (matches the suffix "DAE")
untangle2(primates2017, "DAE$", scientific_name, family)
#> 11 matches
#> # A tibble: 58 × 5
#>    scientific_name              common_name       red_list_status mass_kg family
#>    <chr>                        <chr>             <chr>           <chr>   <chr> 
#>  1 Asia                         NA                NA              NA      NA    
#>  2 Trachypithecus obscurus      Dusky Langur      NT              7.13    CERCO…
#>  3 Presbytis sumatra            Black Sumatran L… EN              6       CERCO…
#>  4 Rhinopithecus roxellana      Golden Snub-nose… EN              NA      CERCO…
#>  5 Trachypithecus auratus       East Javan Langur VU              6.25    CERCO…
#>  6 Semnopithecus johnii         Nilgiri Langur    VU              11.45   CERCO…
#>  7 Trachypithecus delacouri     Delacour's Langur CR              NA      CERCO…
#>  8 Trachypithecus leucocephalus White-headed Lan… CR              8       CERCO…
#>  9 Presbytis comata             Javan Langur      EN              6.7     CERCO…
#> 10 Macaca pagensis              Pagai Macaque     CR              4.5     CERCO…
#> # ℹ 48 more rows
# put geographic regions in their own variable (matching them all by name)
untangle2(
  primates2017, "Asia|Madagascar|Mainland Africa|Neotropics",
  scientific_name, family
)
#> 4 matches
#> # A tibble: 65 × 5
#>    scientific_name              common_name       red_list_status mass_kg family
#>    <chr>                        <chr>             <chr>           <chr>   <chr> 
#>  1 CERCOPITHECIDAE              NA                NA              NA      Asia  
#>  2 Trachypithecus obscurus      Dusky Langur      NT              7.13    Asia  
#>  3 Presbytis sumatra            Black Sumatran L… EN              6       Asia  
#>  4 Rhinopithecus roxellana      Golden Snub-nose… EN              NA      Asia  
#>  5 Trachypithecus auratus       East Javan Langur VU              6.25    Asia  
#>  6 Semnopithecus johnii         Nilgiri Langur    VU              11.45   Asia  
#>  7 Trachypithecus delacouri     Delacour's Langur CR              NA      Asia  
#>  8 Trachypithecus leucocephalus White-headed Lan… CR              8       Asia  
#>  9 Presbytis comata             Javan Langur      EN              6.7     Asia  
#> 10 Macaca pagensis              Pagai Macaque     CR              4.5     Asia  
#> # ℹ 55 more rows
# with magrittr pipes (re-exported in this package)
primates2017 %>%
  untangle2("DAE$", scientific_name, family) %>%
  untangle2(
    "Asia|Madagascar|Mainland Africa|Neotropics",
    scientific_name, region
  )
#> 11 matches
#> 4 matches
#> # A tibble: 54 × 6
#>    scientific_name             common_name red_list_status mass_kg family region
#>    <chr>                       <chr>       <chr>           <chr>   <chr>  <chr> 
#>  1 Trachypithecus obscurus     Dusky Lang… NT              7.13    CERCO… Asia  
#>  2 Presbytis sumatra           Black Suma… EN              6       CERCO… Asia  
#>  3 Rhinopithecus roxellana     Golden Snu… EN              NA      CERCO… Asia  
#>  4 Trachypithecus auratus      East Javan… VU              6.25    CERCO… Asia  
#>  5 Semnopithecus johnii        Nilgiri La… VU              11.45   CERCO… Asia  
#>  6 Trachypithecus delacouri    Delacour's… CR              NA      CERCO… Asia  
#>  7 Trachypithecus leucocephal… White-head… CR              8       CERCO… Asia  
#>  8 Presbytis comata            Javan Lang… EN              6.7     CERCO… Asia  
#>  9 Macaca pagensis             Pagai Maca… CR              4.5     CERCO… Asia  
#> 10 Trachypithecus germaini     Germain's … EN              8.83    CERCO… Asia  
#> # ℹ 44 more rows