Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Convert List Column to String of Comma-Separated Values

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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 = ", ")))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading