Skip to contents

Unwrap values and clean up NAs used as padding

Usage

unwrap_cols(df, groupingVar, separator)

Arguments

df

A data frame with wrapped values and an inconsistent number of NA values used to as within-group padding.

groupingVar

Name of the variable describing the observational units.

separator

Character string defining the separator that will delimit the elements of the unwrapped value.

Value

A summarized tibble. Order is preserved in the grouping variable by making it a factor.

Details

This is roughly the opposite of tidyr::separate_rows().

Examples

data(primates2017_wrapped)
# using commas to separate elements
unwrap_cols(primates2017_wrapped, scientific_name, ", ")
#> # A tibble: 2 × 6
#>   scientific_name         common_name    habitat red_list_status mass_kg country
#>   <chr>                   <chr>          <chr>   <chr>           <chr>   <chr>  
#> 1 Trachypithecus germaini Germain's Lan… evergr… EN              8.83    Cambod…
#> 2 Nycticebus menagensis   Philippine Sl… lowlan… VU              0.28    Brunei…

# separating with semicolons
df <- data.frame(
  ounits = c("A", NA, "B", "C", "D", NA),
  vals = c(1, 2, 2, 3, 1, 3)
)
unwrap_cols(df, ounits, ";")
#> # A tibble: 4 × 2
#>   ounits vals 
#>   <chr>  <chr>
#> 1 A      1;2  
#> 2 B      2    
#> 3 C      3    
#> 4 D      1;3