Home > other >  What kind of extraction for this type of variable in R
What kind of extraction for this type of variable in R

Time:12-09

In R

How can extract data fron this type of variable "['Ralph Macchio, ', 'William Zabka, ', 'Courtney Henggeler, ', 'Xolo Maridueña']" for having a simple liste like "Ralph Macchio, William Zabka, Courtney Henggeler, Xolo Maridueña" ?

Thanks

Dont know because its not a Json format ?

Its for the stars variable in this data frame

structure(list(
title = c("Cobra Kai", "The Crown", "Better Call Saul", "Devil in Ohio", "Cyberpunk: Edgerunners", "The Sandman"), 
year = c("(2018– )", "(2016– )", "(2015–2022)", "(2022)", "(2022– )", "(2022– )"), certificate = c("TV-14", "TV-MA", "TV-MA", "TV-MA", "TV-MA", "TV-MA"), 
duration = c("30 min", "58 min", "46 min", "356 min", "24 min", "45 min"), 
genre = c("Action, Comedy, Drama", "Biography, Drama, History", "Crime, Drama", "Drama, Horror, Mystery", "Animation, Action, Adventure", "Drama, Fantasy, Horror"), 
rating = c(8.5, 8.7, 8.9, 5.9, 8.6, 7.8), 
stars = c("['Ralph Macchio, ', 'William Zabka, ', 'Courtney Henggeler, ', 'Xolo Maridueña']", "['Claire Foy, ', 'Olivia Colman, ', 'Imelda Staunton, ', 'Matt Smith']", "['Bob Odenkirk, ', 'Rhea Seehorn, ', 'Jonathan Banks, ', 'Patrick Fabian']", "['Emily Deschanel, ', 'Sam Jaeger, ', 'Gerardo Celasco, ', 'Madeleine Arthur']", "['Zach Aguilar, ', 'Kenichiro Ohashi, ', 'Emi Lo, ', 'Aoi Yûki']", "['Tom Sturridge, ', 'Boyd Holbrook, ', 'Patton Oswalt, ', 'Vivienne Acheampong']"), 
votes = c(177031, 199885, 501384, 9773, 15413, 116358), 
yearfirst = c("2018", "2016", "2015", "2022", "2022", "2022")), row.names = c(NA, -6L
), class = c("tbl_df", "tbl", "data.frame"))

CodePudding user response:

Is this what you are looking for?

library(tidyverse)

df |> 
  mutate(stars = gsub("[][]", '', stars)) |> 
  mutate(stars = gsub("', '", '', stars)) |> 
  select(stars)
#> # A tibble: 6 × 1
#>   stars                                                             
#>   <chr>                                                             
#> 1 'Ralph Macchio, William Zabka, Courtney Henggeler, Xolo Maridueña'
#> 2 'Claire Foy, Olivia Colman, Imelda Staunton, Matt Smith'          
#> 3 'Bob Odenkirk, Rhea Seehorn, Jonathan Banks, Patrick Fabian'      
#> 4 'Emily Deschanel, Sam Jaeger, Gerardo Celasco, Madeleine Arthur'  
#> 5 'Zach Aguilar, Kenichiro Ohashi, Emi Lo, Aoi Yûki'                
#> 6 'Tom Sturridge, Boyd Holbrook, Patton Oswalt, Vivienne Acheampong'
  • Related