6

My data consist of an identifier (srdr_id), and a list column.

dat <- structure(list(srdr_id = c("174136", "174258", "174684"), outcomes = list(
    structure(list(outcome_s = c("use_alcohol", "use_cannabis", 
    "use_cocaine")), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, 
    -3L)), structure(list(outcome_s = "use_methamphetamine"), class = c("tbl_df", 
    "tbl", "data.frame"), row.names = c(NA, -1L)), structure(list(
        outcome_s = c("use_alcohol", "use_heavy")), class = c("tbl_df", 
    "tbl", "data.frame"), row.names = c(NA, -2L)))), class = c("tbl_df", 
"tbl", "data.frame"), row.names = c(NA, -3L))

> dat
# A tibble: 3 x 2
  srdr_id outcomes        
  <chr>   <list>          
1 174136  <tibble [3 x 1]>
2 174258  <tibble [1 x 1]>
3 174684  <tibble [2 x 1]>

I would like to convert each tibble in outcomes to a single comma separated string.

enter image description here

user25494
  • 1,289
  • 14
  • 27
  • 2
    If you call `unnest` on the data frame, it becomes a dupe of [this](https://stackoverflow.com/q/15933958/5325862) – camille May 13 '19 at 23:14

3 Answers3

6

You can now use

dat %>% rowwise() %>% 
    mutate(outcomes = paste(outcomes, collapse=',')) %>%
    ungroup()
geotheory
  • 22,624
  • 29
  • 119
  • 196
  • 1
    This solution worked great for me, but as a note for anyone using it, you may have to unlist() the column within paste if you don't want the c() notation around your values. – Lia_G Apr 26 '22 at 20:56
4

You can also use a map function to iterate over the list-column, pulling out the first column of each tibble and collapsing into a single string:

library(tidyverse)
dat <- structure(list(srdr_id = c("174136", "174258", "174684"), outcomes = list(structure(list(outcome_s = c("use_alcohol", "use_cannabis", "use_cocaine")), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, -3L)), structure(list(outcome_s = "use_methamphetamine"), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, -1L)), structure(list(outcome_s = c("use_alcohol", "use_heavy")), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, -2L)))), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, -3L))
dat %>%
  mutate(outcomes = map_chr(outcomes, ~ .[[1]] %>% str_c(collapse = ", ")))
#> # A tibble: 3 x 2
#>   srdr_id outcomes                              
#>   <chr>   <chr>                                 
#> 1 174136  use_alcohol, use_cannabis, use_cocaine
#> 2 174258  use_methamphetamine                   
#> 3 174684  use_alcohol, use_heavy

Created on 2019-05-13 by the reprex package (v0.2.1)

Calum You
  • 14,687
  • 4
  • 23
  • 42
3

Here's a way with tidyverse -

dat %>% 
  unnest(outcomes) %>% 
  group_by(srdr_id) %>% 
  summarise(
    outcomes = toString(outcome_s)
  )

# A tibble: 3 x 2
  srdr_id outcomes                              
  <chr>   <chr>                                 
1 174136  use_alcohol, use_cannabis, use_cocaine
2 174258  use_methamphetamine                   
3 174684  use_alcohol, use_heavy        
Shree
  • 10,835
  • 1
  • 14
  • 36