Home > Software engineering >  Convert lists in a dataframe column into a single list in R
Convert lists in a dataframe column into a single list in R

Time:07-31

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"

CodePudding user response:

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"

CodePudding user response:

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
>
  •  Tags:  
  • r
  • Related