Collapses list columns in a tibble into columns of character scalars.

collapse_to_string(.data, ..., .sep = ", ")

Arguments

.data

Input tibble to modify

...

One or more unquoted expressions separated by commas (in the style of dplyr::select(), etc.). Variable names can be used as if they were positions in the data frame, so expressions like x:y can be used to select a range of variables.

.sep

Character scalar to use a separator when collapsing vectors. Defaults to ", ".

Value

Returns the same tibble as .data, but any list columns named in ... will be collapsed to a character column, with a character scalar (or NA) for each row.

Cells containing either character, numeric, or logical vectors will be collapsed to a single string with paste0(collapse = .sep).

Cells containing anything else (i.e. lists, nested tibbles, etc.) will be converted to a string representation of that object via dput().

Details

Any non-list columns passed to ... will be ignored and will trigger a warning notifying the user that only list columns can be collapsed.

Examples

df <- tibble::tibble(
  row_num   = c(1, 2, 3),
  char_list = list(c("foo", "bar"), "baz", c("naw", "dawg")),
  num_list  = list(c(23, 34, 45), c(-1, -2, -3), NULL),
  list_list  = list(list(a=1, b=2, c=3), NULL, list(z=-1, y=-2, x=-3))
)

collapse_to_string(df, char_list, num_list, list_list)
#> # A tibble: 3 × 4
#>   row_num char_list num_list   list_list                   
#>     <dbl> <chr>     <chr>      <chr>                       
#> 1       1 foo, bar  23, 34, 45 list(a = 1, b = 2, c = 3)   
#> 2       2 baz       -1, -2, -3 NA                          
#> 3       3 naw, dawg NA         list(z = -1, y = -2, x = -3)