Search code examples
rlisttibblemutate

How to make a single string column by pasting from list vector column?


I am trying to clean up table to use as an index. Since some of the entries correspond to multiple columns I wanted to concatenate where necessary into a single string. Example of what I am trying to do is:

tibble(
 day = c("Mo", "Tu","Tu", "We","Th","Fr","Fr","Fr"),
 see = c("cat", "cat", "dog", NA, "cat","cat","dog","bird" )
) %>% 
 nest(data = see) %>% 
 mutate(see = paste0(data)) %>%  
 select(-data)

This almost works, but the paste0 doesn't just paste the data from the list, but rather:

# A tibble: 5 × 2
  day   see                                        
  <chr> <chr>                                      
1 Mo    "list(see = \"cat\")"                      
2 Tu    "list(see = c(\"cat\", \"dog\"))"          
3 We    "list(see = NA)"                           
4 Th    "list(see = \"cat\")"                      
5 Fr    "list(see = c(\"cat\", \"dog\", \"bird\"))"

What do I need to change? I want the see column to contain only comma or space separated strings or remain empty where NA.


Solution

  • I think we don't need nest(), but can just summarise() with paste(..., collapse = ", ") instead.

    library(dplyr)
    
    tibble(
      day = c("Mo", "Tu","Tu", "We","Th","Fr","Fr","Fr"),
      see = c("cat", "cat", "dog", NA, "cat","cat","dog","bird" )
    ) %>% 
      summarise(data = paste(see, collapse = ", "), .by = "day")
    
    #> # A tibble: 5 × 2
    #>   day   data          
    #>   <chr> <chr>         
    #> 1 Mo    cat           
    #> 2 Tu    cat, dog      
    #> 3 We    NA            
    #> 4 Th    cat           
    #> 5 Fr    cat, dog, bird
    

    Created on 2023-03-16 with reprex v2.0.2