Unbreak values using regex to match the broken half of the value

unbreak_vals(df, regex, ogcol, newcol, sep = " ", .slice_groups = FALSE)

Arguments

df

A data frame with one or more values within a variable broken up across two rows.

regex

Regular expression for matching the second half of the broken values.

ogcol

Variable to unbreak.

newcol

Name of the new variable with the unified values.

sep

Character string to separate the unified values (default is space).

.slice_groups

When `.slice_groups = FALSE` (the default), the extra rows and the variable with broken values will not be dropped.

Value

A tibble with unbroken values. The variable that originally contained the broken values gets dropped, and the new variable with the unified values is placed as the first column.

Details

This function is limited to quite specific cases, but useful when dealing with tables that contain scientific names broken across two rows. For unwrapping values, see unwrap_cols.

Examples

data(primates2017_broken) # regex matches strings starting in lowercase (broken species epithets) unbreak_vals(primates2017_broken, "^[a-z]", scientific_name, sciname_new)
#> # A tibble: 19 x 5 #> scientific_name common_name red_list_status mass_kg sciname_new #> <chr> <chr> <chr> <dbl> <chr> #> 1 Semnopithecus joh… Nilgiri Langur VU 11.4 Semnopithecus j… #> 2 Trachypithecus Dusky Langur NT 7.13 Trachypithecus #> 3 obscurus NA NA NA Trachypithecus … #> 4 Presbytis sumatra Black Sumatran L… EN 6 Presbytis sumat… #> 5 Trachypithecus au… East Javan Langur VU 6.25 Trachypithecus … #> 6 Trachypithecus de… Delacour's Langur CR NA Trachypithecus … #> 7 Trachypithecus White-headed Lan… CR 8 Trachypithecus #> 8 leucocephalus NA NA NA Trachypithecus … #> 9 Presbytis comata Javan Langur EN 6.7 Presbytis comata #> 10 Macaca pagensis Pagai Macaque CR 4.5 Macaca pagensis #> 11 Trachypithecus ge… Germain's Langur EN 8.83 Trachypithecus … #> 12 Macaca munzala Arunachal Macaque EN NA Macaca munzala #> 13 Macaca mulatta Rhesus Macaque LC 9.9 Macaca mulatta #> 14 Semnopithecus hec… Terai Sacred Lan… NT 15.2 Semnopithecus h… #> 15 Hylobates klossii Kloss's Gibbon EN 5.8 Hylobates kloss… #> 16 Nycticebus menage… Philippine Slow … VU 0.28 Nycticebus mena… #> 17 Nycticebus Bengal Slow Loris VU 1.21 Nycticebus #> 18 bengalensis NA NA NA Nycticebus beng… #> 19 Nomascus concolor Western Black Cr… CR 7.71 Nomascus concol…