Unnesting List Expressed As String In Data Frame Column In R

We can use separate_rows after removing the square brackets and the quotes

library(dplyr)
library(tidyr)
library(stringr)
raw_df %>% 
  mutate(categories = str_remove_all(categories, '\\[|\\]|"')) %>% 
  separate_rows(categories)

-output

# A tibble: 8 x 2
#  movie_id categories
#     <dbl> <chr>     
#1        1 Romance   
#2        1 Comedy    
#3        1 Holiday   
#4        2 Romance   
#5        2 Comedy    
#6        3 Horror    
#7        4 Action    
#8        4 Thriller  

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top