Kusisi Karem
Kusisi Karem

Reputation: 107

Convert lists in a dataframe column into a single list in R

I have a dataframe named df with only a column named Reviews:

Reviews
"Good, Excellent, I love this!"
"This is great, quality is good"
"Excellent service and quality, Good, amazing"

How could I convert the column into a single list as follows?:

Reviews 
"Good, Excellent, I love this!", "This is great, quality is good", "Excellent service and quality, Good, amazing"

I tried unnest but it is not suitable in this case as it returns Error in UseMethod("unnest"): no applicable method for 'unnest' applied to an object of class "list"

Upvotes: 2

Views: 129

Answers (2)

TarJae
TarJae

Reputation: 78917

Update: OP Request: We can do it this way:

df %>% 
  mutate(Reviews = paste0('"',Reviews,'"')) %>% 
  summarise(Reviews = toString(Reviews))

 Reviews
1 "Good, Excellent, I love this!", "This is great, quality is good", "Excellent service and quality, Good, amazing"
> 

First answer: We could use toString with summarise

library(dplyr)
df %>% 
  summarise(Reviews = toString(Reviews))
1 Good, Excellent, I love this!, This is great, quality is good, Excellent service and quality, Good, amazing
>

Upvotes: 2

Mohamed Desouky
Mohamed Desouky

Reputation: 4425

Try this

paste0(df$Reviews , collapse = ", ")
  • output
[1] "Good, Excellent, I love this!, This is great, quality is good, Excellent service and quality, Good, amazing"

Upvotes: 2

Related Questions