Skip to contents

Convert a list-type column (typically in a tibble) to a character-type column. Often useful for plotting along this column.

Usage

list_col_to_chr(list_col, name = NULL, verbatim = FALSE)

Arguments

list_col

A list-type column to be converted to character.

name

Name of column. Used as a prefix in the returned character strings. Default is NULL, which adds no prefix.

verbatim

If TRUE, paste list contents together into a character vector. If FALSE (default), map items in list to unique identifiers (i.e., 1, 2, 3, ...).

Value

A character vector of the same length as list_col.

Examples

# create example tibble with list columns to convert
plot_tib <- tibble::tibble(a = 1:3,
                           b = list(1, 2, 3),
                           c = list(1:2, 3:4, 5:6),
                           d = list(tibble::tibble(a = 1:3,
                                                   b = c(4, 5, 6)),
                                    "abc",
                                    123))
# verbatim = TRUE pastes lists together verbatim
plot_tib_chr_verbatim <- plot_tib %>%
  dplyr::mutate(dplyr::across(tidyselect::everything(),
                              ~list_col_to_chr(.x,
                                               name = dplyr::cur_column(),
                                               verbatim = TRUE)))

# verbatim = FALSE maps items in list to unique ids (i.e., 1, 2, 3, ...)
plot_tib_chr <- plot_tib %>%
  dplyr::mutate(dplyr::across(tidyselect::everything(),
                              ~list_col_to_chr(.x,
                                               name = dplyr::cur_column(),
                                               verbatim = FALSE)))