I have the following data.frame. How can I convert the color_names column to a comma-separated string? A dplyr solution is preferable.
tibble::tibble(
col_hex = c("#0087BF", "#FF0000"),
col_freq = c(87462L, 87019L),
col_share = c(0.501269479198308, 0.498730520801692),
color_names = list('deepskyblue3', c("red", "red1"))
)
#> # A tibble: 2 x 4
#> col_hex col_freq col_share color_names
#> <chr> <int> <dbl> <list>
#> 1 #0087BF 87462 0.501 <chr [1]>
#> 2 #FF0000 87019 0.499 <chr [2]>
My desired output is below.
#> # A tibble: 2 x 4
#> col_hex col_freq col_share color_names
#> <chr> <int> <dbl> <chr>
#> 1 #0087BF 87462 0.501 deepskyblue3
#> 2 #FF0000 87019 0.499 red, red1
>Solution :
Does this work?
tibble::tibble(
col_hex = c("#0087BF", "#FF0000"),
col_freq = c(87462L, 87019L),
col_share = c(0.501269479198308, 0.498730520801692),
color_names = list('deepskyblue3', c("red", "red1"))
) %>%
mutate(color_names = map_chr(color_names, ~str_c(.x, collapse = ", ")))