collapse_to_string.Rd
Collapses list columns in a tibble into columns of character scalars.
collapse_to_string(.data, ..., .sep = ", ")
.data | Input tibble to modify |
---|---|
... | One or more unquoted expressions separated by commas (in the style
of |
.sep | Character scalar to use a separator when collapsing vectors.
Defaults to |
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()
.
Any non-list columns passed to ...
will be ignored and will
trigger a warning notifying the user that only list columns can be collapsed.
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 x 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)