Skip to contents

Function to expand list columns into additional rows.

Usage

expand_columns(
  data,
  columns,
  separator = ",",
  unite_with = NULL,
  remove_cols = FALSE
)

Arguments

data

A data frame to use.

columns

Character vector containing the names of the columns that will be used for the expansion of the data frame.

separator

Character vector, containing the separator that is used to distinguish different elements in the list columns.

unite_with

Character string. Name of

remove_cols

A logical, indicating if the columns need to be removed from the data set after expansion. Will be ignored if unite_with is NULL, to ensure that the information in columns is not lost. Can be a vector of logicals if multiple columns are used for expansion.

Value

A data frame that usually will be longer than the original one.

Examples

 df <- head(iris, n = 6) |> 
  dplyr::mutate(
    expansion_1 = list("feature.1, feature.2"), 
    expansion_2 = list("group.1, group.2")
    )
    
 # expand the data frame with the values in columns "expansion_1" and "expansion_2":
  expand_columns(df, c("expansion_1", "expansion_2"))
#> # A tibble: 24 × 7
#>    Sepal.Length Sepal.Width Petal.Length Petal.Width Species expansion_1
#>           <dbl>       <dbl>        <dbl>       <dbl> <fct>   <chr>      
#>  1          5.1         3.5          1.4         0.2 setosa  feature.1  
#>  2          5.1         3.5          1.4         0.2 setosa  feature.1  
#>  3          5.1         3.5          1.4         0.2 setosa  feature.2  
#>  4          5.1         3.5          1.4         0.2 setosa  feature.2  
#>  5          4.9         3            1.4         0.2 setosa  feature.1  
#>  6          4.9         3            1.4         0.2 setosa  feature.1  
#>  7          4.9         3            1.4         0.2 setosa  feature.2  
#>  8          4.9         3            1.4         0.2 setosa  feature.2  
#>  9          4.7         3.2          1.3         0.2 setosa  feature.1  
#> 10          4.7         3.2          1.3         0.2 setosa  feature.1  
#> # ℹ 14 more rows
#> # ℹ 1 more variable: expansion_2 <chr>

# You can also unite the values in the expansion columns with any column in the data frame, 
# and remove column "expansion_1" but keep column "expansion_2" afterwards:
expand_columns(df, c("expansion_1", "expansion_2"),  
  unite_with = "Species", remove_cols = c(TRUE, FALSE))
#> # A tibble: 24 × 6
#>    Sepal.Length Sepal.Width Petal.Length Petal.Width Species         expansion_2
#>           <dbl>       <dbl>        <dbl>       <dbl> <chr>           <chr>      
#>  1          5.1         3.5          1.4         0.2 setosa_feature… group.1    
#>  2          5.1         3.5          1.4         0.2 setosa_feature… group.2    
#>  3          5.1         3.5          1.4         0.2 setosa_feature… group.1    
#>  4          5.1         3.5          1.4         0.2 setosa_feature… group.2    
#>  5          4.9         3            1.4         0.2 setosa_feature… group.1    
#>  6          4.9         3            1.4         0.2 setosa_feature… group.2    
#>  7          4.9         3            1.4         0.2 setosa_feature… group.1    
#>  8          4.9         3            1.4         0.2 setosa_feature… group.2    
#>  9          4.7         3.2          1.3         0.2 setosa_feature… group.1    
#> 10          4.7         3.2          1.3         0.2 setosa_feature… group.2    
#> # ℹ 14 more rows