Rectangling embedded subheaders

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 x 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 CERCOPITH… #> 3 Presbytis sumatra Black Sumatran La… EN 6 CERCOPITH… #> 4 Rhinopithecus roxellana Golden Snub-nosed… EN NA CERCOPITH… #> 5 Trachypithecus auratus East Javan Langur VU 6.25 CERCOPITH… #> 6 Semnopithecus johnii Nilgiri Langur VU 11.45 CERCOPITH… #> 7 Trachypithecus delacou… Delacour's Langur CR NA CERCOPITH… #> 8 Trachypithecus leucoce… White-headed Lang… CR 8 CERCOPITH… #> 9 Presbytis comata Javan Langur EN 6.7 CERCOPITH… #> 10 Macaca pagensis Pagai Macaque CR 4.5 CERCOPITH… #> # … with 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 x 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 Lang… EN 6 Asia #> 4 Rhinopithecus roxellana Golden Snub-nosed M… 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 leucoceph… White-headed Langur CR 8 Asia #> 9 Presbytis comata Javan Langur EN 6.7 Asia #> 10 Macaca pagensis Pagai Macaque CR 4.5 Asia #> # … with 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 x 6 #> scientific_name common_name red_list_status mass_kg family region #> <chr> <chr> <chr> <chr> <chr> <chr> #> 1 Trachypithecus obsc… Dusky Langur NT 7.13 CERCOPIT… Asia #> 2 Presbytis sumatra Black Sumatran… EN 6 CERCOPIT… Asia #> 3 Rhinopithecus roxel… Golden Snub-no… EN NA CERCOPIT… Asia #> 4 Trachypithecus aura… East Javan Lan… VU 6.25 CERCOPIT… Asia #> 5 Semnopithecus johnii Nilgiri Langur VU 11.45 CERCOPIT… Asia #> 6 Trachypithecus dela… Delacour's Lan… CR NA CERCOPIT… Asia #> 7 Trachypithecus leuc… White-headed L… CR 8 CERCOPIT… Asia #> 8 Presbytis comata Javan Langur EN 6.7 CERCOPIT… Asia #> 9 Macaca pagensis Pagai Macaque CR 4.5 CERCOPIT… Asia #> 10 Trachypithecus germ… Germain's Lang… EN 8.83 CERCOPIT… Asia #> # … with 44 more rows