collapse_to_string: Collapse list column to a character column

View source: R/collapse-to-string.R

collapse_to_stringR Documentation

Collapse list column to a character column

Description

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

Usage

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 ", ".

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.

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().

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)


metrumresearchgroup/rbabylon documentation built on April 21, 2024, 3:26 a.m.