
Last chance! 50% off unlimited learning
Sale ends in
Rectangling embedded subheaders
untangle2(df, regex, orig, new)
A tibble without the matched subheaders and a new variable containing the grouping data.
A data frame with embedded subheaders.
Regular expression to match the subheaders.
Variable containing the extraneous subheaders.
Name of variable that will contain the group values.
Special thanks to Jenny Bryan for fixing the initial tidyeval code and overall function structure.
data(primates2017)
# put taxonomic family in its own variable (matches the suffix "DAE")
untangle2(primates2017, "DAE$", scientific_name, family)
# put geographic regions in their own variable (matching them all by name)
untangle2(
primates2017, "Asia|Madagascar|Mainland Africa|Neotropics",
scientific_name, family
)
# with magrittr pipes (re-exported in this package)
primates2017 %>%
untangle2("DAE$", scientific_name, family) %>%
untangle2(
"Asia|Madagascar|Mainland Africa|Neotropics",
scientific_name, region
)
Run the code above in your browser using DataLab